簡體   English   中英

如何在ArrayList中選擇元素的索引並更改其屬性之一?

[英]How to select index of an element in an ArrayList and change one of it's properties?

我想更改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

get從列表中get相關對象,然后直接對其進行操作:

rectangles.get(5).infected = true;

看看這個:

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]]

我分析你的問題。 您必須在一個變量中獲取該索引的所有值,以便可以對其進行編輯。 最好使用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}]

您需要首先從ArrayList中獲取要更改的對象。 請記住,ArrayList只是對象的容器。 一旦獲得所需的對象,就可以像處理對象一樣處理它。 因此可以是:

RectangleL rectangle = rectangles.get(5);   
rectangle.infected = true;

除此之外,還建議您在類中創建一個getter和setter方法,並將受感染的字段設為私有,以便您可以更好地控制如何訪問和修改字段。

祝您好運

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM