在数组中通过索引查找对象引用

huangapple 未分类评论49阅读模式
英文:

finding object reference in array by index

问题

正如标题所述,我正在尝试通过传入显式索引值并在数组中搜索以确定索引/对象是否存在,判断是否存在,然后如果存在,则返回该值/对象引用,否则返回 null。

for (int i = 0; i < nextPos - 1; i++) {
    if (i == inIndex) {
        i = inIndex;
        return results[i];
    }
}
// 如果没有找到,应该执行以下操作
return null;

nextPos 是表示数组中有多少个“槽位”填充的伴随变量。

inIndex 是显式索引。

在我的代码中,我假设它会遍历整个循环,如果当前位置 i 等于索引,则应该将 i 设置为 inIndex,并返回相应的值,但我一直得到 null 结果。

我已经确认数组已经填充了 50 个值。

我不知道我在这里做错了什么。
数组 results 是持有数组的对象引用。

更新:nextPos 已经设置为等于数组 results 中的填充位置

{
    if (this.nextPos < this.results.length){
        this.nextPos++;
    }
    return nextPos;
}

inIndex 是我正在搜索的索引,
如上所述,我正在尝试在 results 数组中找到对象的索引,使用我传入的 InIndex 值,然后一旦找到,返回该索引处包含的值。如果该索引不存在,则返回 null。

英文:

As the title says i'm trying to determine if a index/object exists by passing in a explicit index value and then searching for it in the array, determining if it exists, then if it exist,then returns the value/object reference there else returns null.

for (int i = 0; i &lt; nextPos -1; i++) {
            if (i == inIndex) {
            i = inIndex;
            return results[i];

            }
// here is what its supposed to do if it doesnt find one
return null;

nextPos is the companion variable for how many "slots" in a array they are filled.

inIndex is the explicit

In my code here I'm assuming it loops throught the entire loop and if i the current position is equal to the index it should set i to equal inIndex; then return whatever there but i just keep getting a null result

i have already confirmed that the array is full with 50 values

i dont know what i am doing wrong here
array results is a object reference holding array

update: nextPos is already set to equal the filled postions in array results

{
     
        
        if (this.nextPos &lt; this.results.length){
            this.nextPos++;
        
    }  

    return nextPos;

inIndex is the index im searching for,
as said above im trying to find the index of a object in results array, using the InIndex i pass in whatever the value is, and then once found return the value contained at that index, if that index does not exist, return null

答案1

得分: 0

根据我理解,您想通过索引在数组中查找对象。请尝试以下代码:

public String findObjectByIndex(int indexToFind) {
    String[] names = {"name1", "name2", "name3", "name4", "name5", "name6", "name7"};

    try {
        return names[indexToFind];
    } catch (ArrayIndexOutOfBoundsException arrayindexException) {
        System.out.println("数组中未找到给定的索引");
    }
    return null;
}
英文:

As per my understanding you are trying to find Object in array by index. Try with below code

public String findObjectByIndex(int indexToFind) {
	String[] names= {&quot;name1&quot;,&quot;name2&quot;,&quot;name3&quot;,&quot;name4&quot;,&quot;name5&quot;,&quot;name6&quot;,&quot;name7&quot;};
	
	try {
		return names[indexToFind];
	}catch(ArrayIndexOutOfBoundsException arrayindexException) {
		System.out.println(&quot;The given index not found in the array&quot;);
	}
	return null;
	
}

huangapple
  • 本文由 发表于 2020年4月5日 13:49:28
  • 转载请务必保留本文链接:https://java.coder-hub.com/61038571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定