2

I have a class Components:

public class Components {

    int numberOfNets; 
    String nameOfComp;
    String nameOfCompPart;
    int numOfPin;

    public components(int i, String compName, String partName, int pin) {
        this.numberOfNets = i;
        this.nameOfComp = compName;
        this.nameOfCompPart = partName; 
        this.numOfPin = pin;
    }

}

Inside another class I created an arraylist of Components class:

List<Components> compList = new ArrayList<Components>();

Later in the code, I am adding the elements in List in this way:

compList.add(new Components(0,compName,partName,0));

See, here numberOfNets and numOfPin variables in Components class are initiated with 0 values. But these values are getting calculated/incremented in a later part of code and hence I need to update the new values of only these two variables in each list element. Now from ArrayList doc I get the idea of updating a list element using its index by set operation. But I am confused how to set/update a particular variable of a class in an ArrayList of a class. I need to update only these two mentioned variables, not all of the four variables in Components class. Is there any way to do that?

0

2 Answers 2

5

You should add getter/setter to your component class so that outer class can update component's members

public class Components {

    private int numberOfNets; 
    private String nameOfComp;
    private String nameOfCompPart;
    private int numOfPin;

    public components(int i, String compName, String partName, int pin) {
        setNumberOfNets(i);
        setNameOfComp(compName);
        setNameOfCompPart(partName); 
        setNumOfPin(pin);
    }

    public void setNumberOfNets(int numberOfNets) {
        this.numberOfNets = numberOfNets;
    }

    // Similarly other getter and setters
}

You can now modify any data by using following code because get() will return reference to original object so modifying this object will update in ArrayList

compList.get(0).setNumberOfNets(newNumberOfNets);
Sign up to request clarification or add additional context in comments.

Comments

3

Example code.

public class Main {

    public static void main(String[] args) {

        List<Components> compList = new ArrayList<Components>();

        compList.add(new Components(0, "compName", "partName", 0));

        System.out.println(compList.get(0).toString());
        
        compList.get(0).numberOfNets = 3;
        compList.get(0).numOfPin = 3;
        
        System.out.println(compList.get(0).toString());     
    }   

}

Your class.

public class Components {

    int numberOfNets;
    String nameOfComp;
    String nameOfCompPart;
    int numOfPin;

    public Components(int i, String compName, String partName, int pin) {
        this.numberOfNets = i;
        this.nameOfComp = compName;
        this.nameOfCompPart = partName;
        this.numOfPin = pin;
    }

    public String toString() {

        return this.numberOfNets + " " + nameOfComp + " " + nameOfCompPart
            + " " + numOfPin;
    }

}

The output:

0 compName partName 0

3 compName partName 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.