我想更改ArrayList
中某个元素的特定属性,但是我不知道如何。我想将其中一个对象的“ infected”属性更改为true。
我尝试在ArrayList上使用'set'方法将我选择的元素中的属性之一设置为其他内容,但是由于某些原因,它不起作用。
rectangles.set(5,infected=false); //I want to change the 'infected' property at element 5 to true.
public class RectangleL{
int sX,sY;
Color color;
int speed;int height; int width;int velX=0;int velY=0;;
public boolean infected=false;
//blah
答案 0 :(得分:2)
您get
从列表中选择相关对象,然后直接对其进行操作:
rectangles.get(5).infected = true;
答案 1 :(得分:1)
只看这个:
public class Main {
public static class RectangleL {
int sX, sY;
String color;
int speed;
int height;
int width;
int velX = 0;
int velY = 0;;
public boolean infected = false;
public int getsX() {
return sX;
}
public void setsX(int sX) {
this.sX = sX;
}
public int getsY() {
return sY;
}
public void setsY(int sY) {
this.sY = sY;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getVelX() {
return velX;
}
public void setVelX(int velX) {
this.velX = velX;
}
public int getVelY() {
return velY;
}
public void setVelY(int velY) {
this.velY = velY;
}
public boolean isInfected() {
return infected;
}
public void setInfected(boolean infected) {
this.infected = infected;
}
@Override
public String toString() {
return "RectangleL [sX=" + sX + ", sY=" + sY + ", color=" + color + ", speed=" + speed + ", height="
+ height + ", width=" + width + ", velX=" + velX + ", velY=" + velY + ", infected=" + infected
+ "]";
}
}
public static void main(String[] args) {
List<RectangleL> rectangles = new ArrayList<>();
// Let say you created a object with infected false;
RectangleL rectangleL = new RectangleL();
rectangleL.setColor("Red");
rectangles.add(0, rectangleL);
System.out.println(rectangles.toString()); // old value
rectangles.get(0).infected = true; // replace by new value
System.out.println(rectangles.toString()); // new value
}
}
输出:
[RectangleL [sX=0, sY=0, color=Red, speed=0, height=0, width=0, velX=0, velY=0, infected=false]]
[RectangleL [sX=0, sY=0, color=Red, speed=0, height=0, width=0, velX=0, velY=0, infected=true]]
答案 2 :(得分:1)
i分析您的问题。您必须在一个变量中获取该索引的所有值,以便可以对其进行编辑。最好的方法是使用HashMap。希望下面的代码对您有所帮助。
import java.util.ArrayList;
import java.util.HashMap;
public class RectangleL {
public static void main(String args[])
{
ArrayList<HashMap<String,String>> lstShap = new ArrayList<>();
HashMap<String,String> shape1 = new HashMap<>();
HashMap<String,String> shape2 = new HashMap<>();
lstShap.clear();
shape1.put("width","30");
shape1.put("height","40");
shape1.put("infected","false");
lstShap.add(0,shape1);
shape2.put("width","20");
shape2.put("height","49");
shape2.put("infected","false");
lstShap.add(1,shape2);
System.out.println(lstShap.toString());
shape1=lstShap.get(1);
System.out.println(shape1.toString());
shape1.put("infected","true");
lstShap.set(1,shape1);
System.out.println(lstShap.toString());
}
}
输出::
[{infected=false, width=30, height=40}, {infected=false, width=20, height=49}]
{infected=false, width=20, height=49}
[{infected=false, width=30, height=40}, {infected=true, width=20, height=49}]
答案 3 :(得分:1)
首先需要从ArrayList中获取要更改的对象。请记住,ArrayList只是对象的容器。一旦获得所需的对象,就可以像处理对象一样处理它。 因此可以是:
RectangleL rectangle = rectangles.get(5);
rectangle.infected = true;
除此之外,还建议您在类中创建一个getter和setter方法,并将受感染的字段设为私有,以便您可以更好地控制如何访问和修改字段。
祝你好运