【发布时间】:2021-07-17 02:31:41
【问题描述】:
假设我有一个名为 sequence 的 int 数组,其中包含值
[1, 3, 2, 4, 3, 5, 4, 6, 1, 3]
我想检查1 的另一个实例除了数组中的第一个之外是否存在,如果存在,我想获取它在数组中的索引。
到目前为止,这就是我所做的:
// Get the first and second number of the sequence array
int firstNumberIndex = 0;
int firstNumber = sequence[0];
int secondNumber = sequence[1];
// Check that firstNumber < secondNumber
if (firstNumber < secondNumber) {
// Remove firstNumber from the sequence array
instance = removeElement(sequence, firstNumberIndex);
// Check whether another instance of
// firstNumber exists in sequence array
if (contains(sequence, firstNumber)) {
// Here, I would like to get the index of
// the other instance of '1', for example
}
}
【问题讨论】:
-
1是任意数字还是总是第一个元素? -
@Bohemian 我想找到数组中第一个元素的另一个出现。在这种情况下,由于
1是第一个元素,我想找到它其他出现的索引
标签: java arrays search integer