JAVA中的ArrayLists

时间:2018-04-02 14:08:04

标签: java

import java.util.ArrayList;

public class Assignment1
{

static ArrayList<Card> cards = new ArrayList<Card>();

public static void main (String[] args)
{

    BasicCard b1 = new BasicCard("IT100","Suren","Bag",100000.00);
                              //ID     NAME    PURCHASE ITEM    BALANCE
    BasicCard b2 = new BasicCard("IT102","Su","Chocolate",150000.00);

    MultiCard m1 = new MultiCard("IT104","Shalinda","Phone",20000.00);
    MultiCard m2 = new MultiCard("IT106" , "Shali" , "Laptop",15000.00);

    cards.add(b1);
    cards.add(b2);
    cards.add(m1);
    cards.add(m2);
    }
}

我想知道如何只访问balance中的ArrayList以及balance是否需要更改。例如,如果我购买另一件商品。 balance需要更新。那么如何更新balance(Double)中的ArrayList呢?

4 个答案:

答案 0 :(得分:0)

您有几个选项,如果您想要更改单个对象的值,您可以通过ArrayList中的索引获取该对象并更改值:

list.get(0).setBalance(2.5);

如果您想对列表中的每个元素执行此操作,可以使用标准for循环或功能forEach

list.forEach(card -> card.setBalance(2.5));

答案 1 :(得分:0)

static ArrayList<Card> cards = new ArrayList<Card>();    
BasicCard b1 = new BasicCard("IT100","Suren","Bag",100000.00);
cards.add(b1)

你可以这样做:

cards.get(0).setBalance(newValue)

它的作用是,如果你的BasicCard有一个setBalance方法,它会将卡片放在索引0并设置余额。

答案 2 :(得分:0)

假设您有Card类的setter和getter。

现在我要做的是首先获取我想要更新其余额的卡的索引,然后更新ArrayList。

public void updateBalance(Double newBalance, String cardName){
    for(Card card : cards){
        if(card.getName().equals(cardName)){
            cards.set(card.indexOf(card),newBalance);
            break;
        }
    }
}

答案 3 :(得分:0)

您需要为列表中的每个属性创建getter和setter,然后相应地更改列表中您想要的索引

import java.util.*; 

//Step 1: Create a Class that have values name and age

class Student { 


    private int age;


    private String name; 

    //Step 2: Creating Constructor of Student class

    public Student( String name, int age) { this.age = age; this.name = name; }

    //Step 3: Getting and Setting the values 

    public int getAge() { return age; } 

    public void setAge(int age) { this.age = age; 

    public String getName() { return name; } 

    public void setName(String name) { this.name = name; } 

    //Step 4: Gives meaning to the object

    @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } 
}

创建主类ArrayListDemo:

import java.util.*; 

//Step 5: Creating the main class 
public class ArrayListDemo{

    public static void main(String args[]){

        //Step 6: Creating objects of Student class

        Student student1= new Student("Vikas" , 24);

        Student student2= new Student("Arun" , 23);

        Student student3= new Student("Rahul" , 25); 

        //Step 7: Creating an object of ArrayList and adding Student objects

        ArrayList<Student> arrayStudent = new
        ArrayList<Student>(); arrayStudent.add(student1); arrayStudent.add(student2); arrayStudent.add(student3); 


        //You can get any object using

        arrayStudent.get(0).getName();

        //Or set name


        arrayStudent.get(0).setName("Rohit");

        //You can update the balance like I am updating the name on zeroth Index

    }
}