简体   繁体   中英

how do we input and save values to ArrayList thorugh setter method in java

This is the setter method for arraylist but dont know how to input through scanner and set the values thorugh this

ArrayList<String> itemNAME;   //Declaration of arrayList

public void setItemNAME(ArrayList<String> itemNAME) {
    this.itemNAME = itemNAME;

and in the other class this is the object

itemNAME = new ArrayList<String>();

Class 1:

ArrayList<String> itemNAME;   //Declaration of arrayList

public void setItemNAME(ArrayList<String> itemNAME) 
{
    this.itemNAME = itemNAME;
}

public void addItemName(String item)
{
    this.itemName.add(item);
}

From there you can call addItemName whenever you need to add an item.

For instance, when you need to add from a scanner, you could store the input from the scanner into a temporary string and then in the next line call addItemName to add the string to your list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM