【问题标题】:Adding an item in ArrayList makes previous items all the same as the newly added在 ArrayList 中添加一个项使之前的项与新添加的项相同
【发布时间】:2013-12-04 21:29:41
【问题描述】:

我是 java 中的菜鸟,我似乎无法弄清楚每次我向 ArrayList 添加新项目时,以前的项目如何与新项目完全相同。我从这里的帖子中学到了同样的问题,但我似乎仍然无法弄清楚真正的问题是什么。我已经在这里待了一个星期了。希望有人能提供帮助。

这是我的代码:

private void generation(String numberOfCase,int i, int j){

    switch(numberOfCase){

    case "N":
        int tempN = 0;

        if(i == 1)
            tempN = 0;
        if(i == 2)
            tempN = 1;

        forGenerating[i][j] = current[tempN][j];
        forGenerating[tempN][j] = 0;

        State tempNo = new State(forGenerating,current,1,howFar);
        adding(tempNo);
        //adding(forGenerating,current,1,howFar);

        forGenerating[tempN][j] = forGenerating[i][j];
        forGenerating[i][j] = 0;

        break;
    case "E":
        int tempE = j+1;

        forGenerating[i][j] = current[i][tempE];
        forGenerating[i][tempE] = 0;

        State tempEa = new State(forGenerating,current,1,howFar);
        adding(tempEa);
        //adding(forGenerating,current,1,howFar);

        forGenerating[i][tempE] = forGenerating[i][j];
        forGenerating[i][j] = 0;

        break;
    case "S":
        int tempS = 0;

        if(i == 0)
            tempS = 1;
        if(i == 1)
            tempS = 2;

        forGenerating[i][j] = current[tempS][j];
        forGenerating[tempS][j] = 0;

        State tempSo = new State(forGenerating,current,1,howFar);
        adding(tempSo);
        //adding(forGenerating,current,1,howFar);

        forGenerating[tempS][j] = forGenerating[i][j];
        forGenerating[i][j] = 0;

        break;
    case "W":
        int tempW = j-1;

        forGenerating[i][j] = current[i][tempW];
        forGenerating[i][tempW] = 0;

        State tempWe = new State(forGenerating,current,1,howFar);
        adding(tempWe);
        //adding(forGenerating,current,1,howFar);

        forGenerating[i][tempW] = forGenerating[i][j];
        forGenerating[i][j] = 0;

        break;
    }

}

private void adding(State temp){

    State t = new State(temp);

    if(closedList.equals(temp) == false){

        forChecking.add(t);
        iterator+=1;

    }

}

我创建了我为州创建的类的 ArrayList。

编辑。这是班级状态。有两个构造函数,因为我已经编辑了这个代码一个星期,并做了我从网上读到的所有可能的解决方案。

导入 java.util.Arrays; 导入 java.util.Random;

公共类状态{

int[][] arr = new int[3][3];
int[][] parent = new int[3][3];
int g=0,f=0,h=0;

public State(int[][] arr, int[][] parent, int g, int h){

    this.arr = Arrays.copyOf(arr, arr.length);
    this.parent = Arrays.copyOf(parent, parent.length);
    this.g = g;
    this.h = h;
    solveF();

}

public State(State temp){

    this.arr = Arrays.copyOf(temp.arr, temp.arr.length);
    this.parent = Arrays.copyOf(temp.parent, temp.parent.length);
    this.g = temp.g;
    this.h = temp.h;
    solveF();

}

private void solveF(){

    f = g+h;

}

}

【问题讨论】:

  • ArrayList在哪里?
  • List forChecking = new ArrayList();在这里,我是这样做的
  • 您的代码应该没有问题。我唯一看到的是,当您执行新状态(临时)时,您必须修改您的类状态的静态成员,这就是为什么您的所有实例都同时被修改(或者至少看起来是)。你能展示你的班级状态吗?
  • 我认为这是我的错误。而且我真的不知道如何不修改数组列表。我尝试了在有关此问题的相关主题上学到的不同建议。谢谢

标签: java arraylist


【解决方案1】:

请注意,在 Java 中,有一个名为 ArrayList 的实际对象。使用它可能会简化您尝试做的事情,或者至少使其更易于阅读,因为该类的方法将使您的代码像动词一样阅读。

值是相同的,因为您将相同的数组存储在新的 State 对象中。考虑Array utilities 中的一种复制方法。稍后在您的代码中,您再次覆盖该值,但由于它是存储的同一个数组对象,它们看起来都一样。

关于您的代码,了解更多您正在尝试做的事情会很有帮助。也许会看到更多周围的代码。但是,有些事情引起了我的注意:

  • 输入变量numberOfCase 是一个字符串,这令人困惑,因为人们可能认为它应该是一个数字。
    • 虽然可以在字符串上使用 switch,但请考虑将这些输入设置为 Enums
  • 在adding() 方法中,代码closedList.equals(temp) 正在比较对象。 “closedList”听起来像一个列表或数组,而“temp”是一个State,所以它们永远不会相等。也许您打算使用!closedList.contains(temp)? (但请注意,您需要在 State 类中覆盖 .equals(Object o)
  • 看起来您的代码假设一个 2x2 值矩阵,并且 i 或 j 将始终为 1 或 2,对应于 N、S、E、W 字符串。对于这种情况,我建议使用简单的方法名称创建您自己的类。也许是这样的:

    public class CompassGenerator{
      //private fields
    
      //returns previous Compass object, if required...
      public Compass generateNorth(int howFar){...}
    
      //Alternatively, pass in Compass object from which to generate
      public Compass generateSouth(Compass prev, int howFar){...}
    
      //etc...
    
    }
    

【讨论】:

  • 谢谢!我要编辑那些部分。这个作业是 8 谜题的 java 实现。 closedList 是 State 对象的数组列表。再次感谢您的建议。我一定会在我的代码中实现它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 1970-01-01
相关资源
最近更新 更多