Java 如何找到数组元素的索引?

Java 如何找到数组元素的索引?,java,indexing,Java,Indexing,我有这个类,它在首选输入后显示数组的值: class MyArrayList { private int [] myArray; private int size; public MyArrayList (int value, int seed, int inc){ myArray = new int [value]; size = value; for (int i = 0; i < size; i++) { myArray [i] = se

我有这个类,它在首选输入后显示数组的值:

class MyArrayList {

private int [] myArray;
private int size;

public MyArrayList (int value, int seed, int inc){

    myArray = new int [value];
    size = value;
    for (int i = 0; i < size; i++) {
        myArray [i] = seed;
        seed += inc;        
    }       
}   

}

public class JavaApp1 {

    public static void main(String[] args) {
        MyArrayList a = new MyArrayList(5, 2, 1);
        a.printList();

}
}
类MyArrayList{
私有int[]myArray;
私有整数大小;
公共MyArrayList(int-value、int-seed、int-inc){
myArray=新整数[值];
大小=值;
对于(int i=0;i
此程序显示以下输出: 2 3 4 5 6
现在我想找到4的索引,这样就等于2了,但是我怎样才能把它放到程序中呢?

您需要在
MyArrayList
类中编写一个方法,该方法获取您要查找的值,并在数组中定位它。要找到它,只需在数组上循环,直到值匹配为止

像这样的

public int findIndexOf(int value){
    for (int i=0; i<size; i++) {
        if (myArray[i] == value){
            return i;
        }
    }    
    return -1; // not found   
}   
public int findIndexOf(int值){

对于(int i=0;i您需要在
MyArrayList
类中编写一个方法,该方法获取您要查找的值,并在数组中定位它。要找到它,只需在数组上循环,直到值匹配为止

像这样的

public int findIndexOf(int value){
    for (int i=0; i<size; i++) {
        if (myArray[i] == value){
            return i;
        }
    }    
    return -1; // not found   
}   
public int findIndexOf(int值){

对于(inti=0;i循环的值,那么您就有了索引,您可能希望它作为MyArrayList类的函数

    int[] myArray = {2,3,4,5,6};
    for (int i = 0; i < myArray.length; i++) 
    {
        if( myArray[i] == 4 )
            System.out.println( "index=" + i);
    }
int[]myArray={2,3,4,5,6};
for(int i=0;i
循环该值,然后您就有了索引,您可能希望将其作为MyArrayList类的函数

    int[] myArray = {2,3,4,5,6};
    for (int i = 0; i < myArray.length; i++) 
    {
        if( myArray[i] == 4 )
            System.out.println( "index=" + i);
    }
int[]myArray={2,3,4,5,6};
for(int i=0;i
Java最好的部分之一是它是开源的,您可以查看所有标准API的源代码

以下是ArrayList.indexOf(Object)方法的工作原理:

/**
 * Returns the index of the first occurrence of the specified element
 * in this list, or -1 if this list does not contain the element.
 * More formally, returns the lowest index <tt>i</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
 * or -1 if there is no such index.
 */
public int indexOf(Object o) {
if (o == null) {
    for (int i = 0; i < size; i++)
    if (elementData[i]==null)
        return i;
} else {
    for (int i = 0; i < size; i++)
    if (o.equals(elementData[i]))
        return i;
}
return -1;
}
/**
*返回指定元素第一次出现的索引
*如果此列表不包含元素,则为-1。
*更正式地说,返回最低索引i,以便
*(o==null?get(i)==null:o.equals(get(i)),
*如果没有此类索引,则为-1。
*/
public int indexOf(对象o){
如果(o==null){
对于(int i=0;i
Java最好的部分之一是它是开源的,您可以查看所有标准API的源代码

以下是ArrayList.indexOf(Object)方法的工作原理:

/**
 * Returns the index of the first occurrence of the specified element
 * in this list, or -1 if this list does not contain the element.
 * More formally, returns the lowest index <tt>i</tt> such that
 * <tt>(o==null&nbsp;?&nbsp;get(i)==null&nbsp;:&nbsp;o.equals(get(i)))</tt>,
 * or -1 if there is no such index.
 */
public int indexOf(Object o) {
if (o == null) {
    for (int i = 0; i < size; i++)
    if (elementData[i]==null)
        return i;
} else {
    for (int i = 0; i < size; i++)
    if (o.equals(elementData[i]))
        return i;
}
return -1;
}
/**
*返回指定元素第一次出现的索引
*如果此列表不包含元素,则为-1。
*更正式地说,返回最低索引i,以便
*(o==null?get(i)==null:o.equals(get(i)),
*如果没有此类索引,则为-1。
*/
public int indexOf(对象o){
如果(o==null){
对于(int i=0;i
您可能还需要记住,这不一定是唯一的属性。嗯,是的,这可能会导致问题,但OP需要澄清他希望如何处理这种情况。目前,它将返回第一个匹配索引。您可能还需要记住,这不一定是唯一的属性。嗯,是的,这可能是c这是一个问题,但OP需要澄清他希望如何处理这种情况。目前,它将返回第一个匹配索引。+1,因为此答案将打印所有查询项目。OP对预期内容不明确,但这种方法似乎更聪明。非常感谢,先生,尽管此代码实际上不起作用k至少我知道怎么做了。+1因为这个答案将打印所有查询项的出现。OP对预期的内容不明确,但这种方法似乎更聪明。非常感谢,先生,尽管这段代码实际上不起作用,但至少我知道怎么做。