Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 查看一组对象_Java_Arrays_Object - Fatal编程技术网

Java 查看一组对象

Java 查看一组对象,java,arrays,object,Java,Arrays,Object,我正在尝试编写一个方法,该方法通过对象数组查找特定颜色的对象,这也是一个对象 public Ghost findFirst(Color c){ for (int i=0;i<ghosts.length;i++) { if (ghosts[i].getColor()==c) return ghosts[i]; else return null; } }

我正在尝试编写一个方法,该方法通过对象数组查找特定颜色的对象,这也是一个对象

public Ghost findFirst(Color c){
        for (int i=0;i<ghosts.length;i++) {
            if (ghosts[i].getColor()==c)
            return ghosts[i];
        else
            return null;
            }  
    }
公共重影findFirst(c色){

对于(int i=0;i,因为您在第一次迭代时从循环返回!所以“i”永远不会递增。请完全删除else块或将“return null”更改为“continue”


作为一个单独的点,==正在检查引用相等性,而不是对象相等性。似乎您应该使用“.equals”,而不是“

,因为您在第一次迭代时从循环返回!因此“i”永远不会递增。请完全删除else块,或者将“return null”更改为“continue”

作为一个单独的点,==正在检查引用相等,而不是对象相等。似乎您应该使用“.equals”来代替它,因为

else 
  return null;
!

由于该返回语句,您的循环将只执行一次。

这是因为

else 
  return null;
public Ghost findFirst(Color c){
    for (int i=0; i < ghosts.length; i++) {
        if (ghosts[i].getColor().equals(c))
            return ghosts[i];
    }  
    return null;
}
!

由于该返回语句,您的循环将只执行一次。

public Ghost findFirst(c色){
public Ghost findFirst(Color c){
    for (int i=0; i < ghosts.length; i++) {
        if (ghosts[i].getColor().equals(c))
            return ghosts[i];
    }  
    return null;
}
for(int i=0;i
公共重影findFirst(c色){
for(int i=0;i
固定代码:

public Ghost findFirst(Color c){
        for (int i=0;i<ghosts.length;i++) {
            if (ghosts[i].getColor().equals(c))
               return ghosts[i];
        }
        return null;
    }
公共重影findFirst(c色){
对于(int i=0;i固定代码:

public Ghost findFirst(Color c){
        for (int i=0;i<ghosts.length;i++) {
            if (ghosts[i].getColor().equals(c))
               return ghosts[i];
        }
        return null;
    }
公共重影findFirst(c色){

对于(int i=0;i如果我“展开”循环,代码执行如下操作:

public Ghost findFirst(Color c){
    if (ghosts[0].getColor()==c)
       return ghosts[0];
    else
       return null;  

    if (ghosts[1].getColor()==c)
       return ghosts[1];
    else
       return null; 

    if (ghosts[2].getColor()==c)
       return ghosts[2];
    else
       return null; 
    //etc...
}

由此可以清楚地看出,如果
,它将永远不会到达第二个
,如果
,它将在第一个
返回(中断函数),是真是假。

如果我“展开”你的循环,代码将执行以下操作:

public Ghost findFirst(Color c){
    if (ghosts[0].getColor()==c)
       return ghosts[0];
    else
       return null;  

    if (ghosts[1].getColor()==c)
       return ghosts[1];
    else
       return null; 

    if (ghosts[2].getColor()==c)
       return ghosts[2];
    else
       return null; 
    //etc...
}

应该清楚地看到,如果
,它将永远不会到达第二个
,如果
,它将在第一个
返回(中断函数),是真是假。

您的多个
返回可能是个问题。有时,让一个
返回变得更简单

public Ghost findFirst(Color c) {
    Color color = null;
    for (int i=0;i<ghosts.length;i++) {
        if (ghosts[i].getColor().equals(c))
        color = ghosts[i];
    }
    return color;   
}
公共重影findFirst(c色){
颜色=空;

对于(int i=0;i您的多个
返回值可能是一个问题。有时,让一个
返回值变得更简单

public Ghost findFirst(Color c) {
    Color color = null;
    for (int i=0;i<ghosts.length;i++) {
        if (ghosts[i].getColor().equals(c))
        color = ghosts[i];
    }
    return color;   
}
公共重影findFirst(c色){
颜色=空;

for(int i=0;i尝试正确缩进代码,然后你自己会看到。但基本上,你是在挫败for循环,如果第一种颜色不是你想要的,你将返回null,甚至不查看所有其他颜色。正如我前面回答的,你将需要使用
equals()
在比较
Color
对象时。尝试正确缩进代码,然后你会看到你自己。但基本上,你是在破坏for循环,如果第一种颜色不是你想要的,你会返回null,甚至不会查看所有其他颜色。正如我前面回答的,你需要使用
equals()
在比较
颜色对象时。我已经修改了我的答案。作为一个关注点,是什么给了你死代码警告?你的IDE?你在使用哪一个?是的,我修复了.equals的东西。我在使用eclipse,I++是带死代码警告的黄色高位代码。但是,有人指示我,如果没有找到任何东西,就返回null。我怎么能在我的例子中,我仍然实现null?另外,当我删除else块时,我仍然得到一个必须返回的Ghost对象errorfprime,您需要将返回null移到循环之外(参见下面SKip Head的代码示例)。基本上,您的逻辑需要如下:“我将遍历这个数组,看看是否能找到我想要的颜色的幽灵。如果我遍历了整个列表,但仍然没有返回幽灵,那么我应该返回null。”…相反,您有“对于列表中的每个幽灵,如果是该颜色,我将返回幽灵,如果不是,则返回null。”。“问题是你只能返回一次,而且你强迫自己立即返回。(因为你在结束前返回,i++永远不会发生).我已经修改了我的答案。作为一个有趣的问题,是什么给了你死代码警告?你的IDE?你在使用哪一个?是的,我修复了.equals的东西。我在使用eclipse,I++是带死代码警告的黄色高位。但是,有人指示我,如果没有找到任何东西,就返回null。我如何在我的情况下仍然实现null?当我rem时也是如此在else块之外,我仍然得到一个必须返回的Ghost对象errorfprime,您需要将返回null移到循环之外(参见下面SKip Head的代码示例)。基本上,您的逻辑需要如下:“我将遍历这个数组,看看是否能找到我想要的颜色的幽灵。如果我遍历了整个列表,但仍然没有返回幽灵,那么我应该返回null。”…相反,您有“对于列表中的每个幽灵,如果是该颜色,我将返回幽灵,如果不是,则返回null。”。“问题是你只能返回一次,而且你强迫自己立即返回。(因为你在结束前返回,i++永远不会发生).哦,好吧,我不知道return结束一个循环。我在上面的修复中使用了messier方法,使用了Boolean,这可能不是必需的。哦,好吧,我不知道return结束一个循环。我在上面的修复中使用了messier方法,使用了Boolean,这可能不是必需的