Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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 在特定类的arraylist中设置元素_Java_Arraylist - Fatal编程技术网

Java 在特定类的arraylist中设置元素

Java 在特定类的arraylist中设置元素,java,arraylist,Java,Arraylist,在我的程序中,我有一个结对类: class Pair { public int ind = 0; public String letter = ""; public Pair(int a, String b) { ind = a; //index letter = b; } } 如何在成对的arraylist中设置元素的索引(ind)?我试过了 RightMotor.ind.set(j, i); 及 但是它们似乎不起作用。在将其添加到ArrayList之前,必

在我的程序中,我有一个结对类:

class Pair {
  public int ind = 0;
  public String letter = "";
  public Pair(int a, String b) {
    ind = a; //index
    letter = b;
  }
}
如何在成对的arraylist中设置元素的索引(ind)?我试过了

RightMotor.ind.set(j, i);


但是它们似乎不起作用。

在将其添加到
ArrayList
之前,必须使用
Pair
构造函数创建
Pair
实例:

RightMotor.add(new Pair(i,j)); // assuming i is an int and j is a String
RightMotor.get(index).setInd(newValue);
如果要替换存储在给定索引中的
,请使用:

RightMotor.set(index,new Pair(i,j));
如果要更改存储在
阵列列表中的现有

RightMotor.add(new Pair(i,j)); // assuming i is an int and j is a String
RightMotor.get(index).setInd(newValue);
这将需要
对中的setter方法
类:

public void setInd (int i) {
    ind = i;
}

首先,您需要“获取”Pair实例,如:

Pair pair = LeftMotor.get(i);
然后可以更改其字段:

pair.ind = j;
这也可以在一行中完成:

LeftMotor.get(i).ind = j;
提示1:这不是更改列表中实例的索引(位置),
LeftMotor.get(i)
仍将返回相同的元素
i
ind
是两个完全不相交的值

提示2:通常最好使用私有字段和方法(setter)来更改字段(封装):


提示3:只是为了澄清,因为它被称为
ind
它不是列表的索引(位置)。如果您想更改列表中元素的顺序,这是一个完全不同的问题。

您想将这些实例变量设置为私有,然后使用getter/setter来访问/修改它们。这使您能够安全可靠地操作数据,同时减少泄漏的可能性(这可能导致程序崩溃或造成意外后果)

在你的班级里:

class Pair {
  private int ind = 0;
  private String letter = "";

  public Pair(int index, String letter) {
    ind = index; 
    letter = letter;
  }

  public int getIndex() {
      return index;
  }

  public void setIndex(int index) {
      this.index = index;
  }

  public String getLetter() {
      return this.letter;
  }

  public void setLetter(String letter) {
      this.letter = letter;
  }

  public void setIndexAndLetter(int index, String letter) {
      this.index = index;
      this.letter = letter;
  }

}
在您的计划中的其他地方:

Pair rightMotor = new Pair(1, "a");
Pair leftMotor = new Pair(2, "b");
Pair middleMotor = new Pair(0, "");

rightMotor.setInd(3);
leftMotor.setLetter("d");
middleMotor.setIndAndLetter(rightMotor.getInd() + leftMotor.getInd(), "z");

所以我认为你可以用几种不同的方法来解决这个问题。。。但是我想我知道你想做什么。。。。我觉得您正在尝试保持ArrayList和Pair索引同步。。。无论哪种方式,您都需要一个助手方法来完成此任务。我同意hendripd的观点,即应该使用私有变量并使用getter和setter。然而,这是我的解决方案

配对:

输出此结果:

0 b
1 d
2 a
3 e
4 c
5 f

LeftMotor和RightMotor是我的成对数组列表,为了将来的参考,您需要包含比您提供的代码更多的代码。与其说不够,不如说提供更多。然而,我相信你有下面的答案。请复习,因为变量不应该以大写字母开头
RightMotor
应该是
RightMotor
LeftMotor
应该是
LeftMotor
。这将使其他人更容易阅读和快速理解你的代码。我想发布一些关于这方面的信息,但你抢先一步。同样,我也发布了一个答案,我认为你是在试图实现。。。但是,由于您没有提供足够的代码,这一点也可以解释here@Frank在这种情况下,使用RightMotor.set(索引,新对(i,j));有没有办法编辑int,谢谢?感谢您的帮助btwMain.java:307:错误:找不到符号LeftMotor.get(j).setInd(i)@Frank您必须将
setInt(inti)
方法添加到您的
Pair
类中。@Frank从类外部更新实例变量不是一个好主意。它破坏了封装。实例变量应该是私有的,并且只能通过方法调用进行修改。
import java.util.ArrayList;
import java.util.Collections;

public class Main {
    public static ArrayList<Pair> rightMotor;

    public static void main(String[] args) {
        rightMotor = new ArrayList<Pair>();

        rightMotor.add(new Pair(0, "a"));
        rightMotor.add(new Pair(1, "b"));

        setIndex(rightMotor, 0, 1);
        // If you choose to go with the second option utilizing Comparable<Pair>
        // Collections.sort(rightMotor);

        for (Pair pair : rightMotor) {
            System.out.println(pair.toString());
        }
    }

    public static void setIndex(ArrayList<Pair> motor, int oldIndex, int newIndex) {
        Pair tempPair = new Pair(motor.get(oldIndex));
        if (oldIndex < newIndex) {
            for (int i = oldIndex; i < newIndex; i++) {
                motor.set(i, motor.get(i + 1));
                motor.get(i).setIndex(i);
            }
        } else if (oldIndex > newIndex) {
            for (int i = oldIndex; i > newIndex; i--) {
                motor.set(i, motor.get(i - 1));
                motor.get(i).setIndex(i);
            }
        }
        tempPair.setIndex(newIndex);
        motor.set(newIndex, tempPair);
    }
}
public static void setIndex(ArrayList<Pair> motor, int oldIndex, int newIndex) {
    motor.get(oldIndex).setIndex(newIndex);
    if (oldIndex < newIndex) {
        for (int i = oldIndex; i < newIndex; i++) {
            motor.get(i + 1).setIndex(i);
        }
    } else if (oldIndex > newIndex) {
        for (int i = oldIndex; i > newIndex; i--) {
            motor.get(i - 1).setIndex(i);
        }
    }
}
    rightMotor.add(new Pair(0, "a"));
    rightMotor.add(new Pair(1, "b"));
    rightMotor.add(new Pair(2, "c"));
    rightMotor.add(new Pair(3, "d"));
    rightMotor.add(new Pair(4, "e"));
    rightMotor.add(new Pair(5, "f"));

    setIndex(rightMotor, 0, 1);
    setIndex(rightMotor, 3, 1);
    setIndex(rightMotor, 4, 3);
0 b
1 d
2 a
3 e
4 c
5 f