猿问

Java空对象形成数组

所以,我有一个程序,我需要获取一个数组对象。所以我必须检查那里的每个对象,但它显示一个错误。我认为这是因为获取空对象不起作用。我该怎么做?我是新来的...


get 是一个简单的返回 this.x,但我认为它因为空值而中断


public Sunflower get(int x, int y) {


boolean found=false;

Sunflower sun = null;


for(int i=0; i<MAX && found==false; i++) {


    if(array[i].getX() == x && array[i].getY() == y) sun= array[i];

}

    return sun;

}

谢谢你的帮助---------------------------编辑


添加 array[i]!=null 不起作用。同样的错误。我认为只是寻找什么都不存在的位置可能会给出问题。我改变了数组大小的最大值,更多的逻辑。我需要检查位置,比如说 (7,8),所以我查看 x 和 y 对象,但我认为如果它没有找到任何东西,它就会给出错误。像这样的东西。:


public void update(){


Sunflower sun = game.getSFinPosition(x, y-1);

if(sun!=null&& sun.getVida()!=0) sun.setLife();

}


如果它发现任何东西,我就会得到分配不起作用,但我尝试将它写在 if 和 nothing 中......所以不知道。


青春有我
浏览 181回答 3
3回答

四季花海

在尝试访问成员变量之前,您应该检查空元素。您也可以使用 break 而不是使用找到的布尔值。公共向日葵获取(int x,int y){Sunflower sun = null;for(int i=0; i<MAX; i++) {&nbsp; &nbsp; if(array[i] && array[i].getX() == x && array[i].getY() == y) {&nbsp; &nbsp; &nbsp; &nbsp; sun= array[i];&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }}return sun;}

手掌心

给定您的代码的工作示例,并进行了一些改进:public class Main {&nbsp; public static class Sunflower {&nbsp; &nbsp; private int x, y;&nbsp; &nbsp; Sunflower(int x, int y) {&nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; int getX() {&nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp; }&nbsp; &nbsp; int getY() {&nbsp; &nbsp; &nbsp; return y;&nbsp; &nbsp; }&nbsp; }&nbsp; public static class Getter {&nbsp; &nbsp; private Sunflower[] array = {new Sunflower(1, 0), new Sunflower(0, 1), null, new Sunflower(3, 1)};&nbsp; &nbsp; Sunflower get(int x, int y) {&nbsp; &nbsp; &nbsp; for (Sunflower s : array) {&nbsp; &nbsp; &nbsp; &nbsp; if (s == null) continue;&nbsp; &nbsp; &nbsp; &nbsp; if (s.getX() == x && s.getY() == y) return s;&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; }&nbsp; public static void main(String[] args) {&nbsp; &nbsp; Getter getter = new Getter();&nbsp; &nbsp; assert getter.get(1, 0) != null;&nbsp; &nbsp; assert getter.get(1, 0) != null;&nbsp; &nbsp; assert getter.get(3, 1) != null;&nbsp; &nbsp; assert getter.get(3, 2) == null;&nbsp; }}您最感兴趣的功能:Sunflower get(int x, int y) {&nbsp; for (Sunflower s : array) {&nbsp; &nbsp; if (s == null) continue;&nbsp; &nbsp; if (s.getX() == x && s.getY() == y) return s;&nbsp; }&nbsp; return null;}变化:带有 foreach 的 for 循环检查值是否为空不设置found变量就返回否则返回 null

莫回无

如果你想检查所有元素,你应该使用array.length来获取数组的最大索引。您也可以检查元素是否为空并跳过它:public Sunflower get( int x, int y ) {&nbsp; &nbsp; boolean found = false;&nbsp; &nbsp; Sunflower sun = null;&nbsp; &nbsp; for (int i = 0; i < array.length && found == false; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (array[i] != null &&&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i].getX() == x && array[i].getY() == y) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sun = array[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; found = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return sun;}
随时随地看视频慕课网APP

相关分类

Java
我要回答