2012-04-07 182 views
28

我有一個Customer存儲在ArrayList中的對象。我的Customer班有2個數據成員:NameEmail。現在我想修改客戶「Doe」的EmailArrayList - 如何修改對象的成員?

現在,如果「李四」位於列表中的索引3,我知道我可以寫這樣一行:

myList.set(3, new Customer("Doe", "[email protected]")); 

但是,這意味着創建一個新對象。如果我有一個非常大的名單,我想這個過程會很慢。有沒有其他方法可以直接訪問存儲在ArrayList中的Object的數據成員,也許通過使用另一種Collection而不是ArrayList?

回答

2

使用myList.get(3)以訪問當前對象,並修改它,假設Customer實例都被修改的方式。

36

你可以這樣做:

myList.get(3).setEmail("new email"); 
1

你可以做一個得到收集,然後只需修改客戶的屬性,你只是做一個「得到」。有沒有需要修改的集合也沒有必要建立一個新的客戶:存儲您收藏的對象

int currentCustomer = 3; 

// get the customer at 3 
Customer c = list.get(currentCustomer); 
// change his email 
c.setEmail("[email protected]"); 
0

如果您需要快速查找(基本上是恆定的時間),你應該使用的不是List地圖。

如果您需要快速迭代您應該使用List的對象。

你的情況

所以......

Map<String,Customer> customers = new HashMap<String,Customer>(); 

//somewhere in the code you fill up the Map, assuming customer names are unique 
customers.put(customer.getName(), customer) 

// at some later point you retrieve it like this; 
// this is fast, given a good hash 
// been calculated for the "keys" in your map, in this case the keys are unique 
// String objects so the default hash algorithm should be fine 
Customer theCustomerYouLookFor = customers.get("Doe"); 

// modify it 
theCustomerYouLookFor.setEmail("[email protected]") 
3

我寫給你2班向你展示它是如何做;主要和客戶。如果您運行的主類,你看這是怎麼回事:

import java.util.*; 

public class Customer { 

    private String name; 
    private String email; 

    public Customer(String name, String email) { 
     this.name = name; 
     this.email = email; 
    } 

    public String getName() { 
     return name; 
    } 

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

    public String getEmail() { 
     return email; 
    } 

    public void setEmail(String email) { 
     this.email = email; 
    } 

    @Override 
    public String toString() { 
     return name + " | " + email; 
    } 

    public static String toString(Collection<Customer> customers) { 
     String s = ""; 
     for(Customer customer : customers) { 
      s += customer + "\n"; 
     } 
     return s; 
    } 

} 


import java.util.*; 

public class Main { 

    public static void main(String[] args) { 
     List<Customer> customers = new ArrayList<>(); 
     customers.add(new Customer("Bert", "[email protected]")); 
     customers.add(new Customer("Ernie", "[email protected]")); 
     System.out.println("customers before email change - start"); 
     System.out.println(Customer.toString(customers)); 
     System.out.println("end"); 
     customers.get(1).setEmail("[email protected]"); 
     System.out.println("customers after email change - start"); 
     System.out.println(Customer.toString(customers)); 
     System.out.println("end"); 
    } 

} 

得到這個跑步,做2班,主要與客戶和複製粘貼來自兩類的正確類的內容;然後運行Main類。

1

您可以遍歷arraylist來識別索引並最終確定需要修改的對象。您可以使用,分別用於以下同:

for(Customer customer : myList) { 
    if(customer!=null && "Doe".equals(customer.getName())) { 
     customer.setEmail("[email protected]"); 
     break; 
    } 
} 

這裏的客戶是目前在ArrayList中的對象的引用,如果你改變這個客戶參考的任何財產,這些變化將反映在存儲在你的對象數組列表。

+0

這不是NPE保存listArrays。 - >「Doe」.equals(customer.getName()), 順便說一句:爲什麼你不會在第一場比賽後停止? – 2012-04-07 10:12:46

+0

感謝您的糾正..這兩點都值得牢記。 – Mahendra 2012-04-08 19:56:03

0

那麼你已經使用Pojo實體,所以你可以做到這一點。 你需要獲得對象,並且必須設置數據。

myList.get(3).setEmail("email"); 

你可以這樣做。或者你也可以設置其他參數。

0

試試這個。當遍歷代碼和修改Arraylist所有元素的字段時,這個工作。

public Employee(String name,String email){ 
this.name=name; 
this.email=email; 
} 

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

public String getName() { 
return name; 
} 

public void setEmail(String email) { 
this.email = email; 
} 

public String getEmail() { 
return email; 
} 

for(int i=0;i++){ 
List.get(i).setName("Anonymous"); 
List.get(i).setEmail("[email protected]"); 
} 
6

固定。我錯了:這隻適用於元素重新分配。我認爲返回的對象沒有引用新的對象。

它可以做到。

問題:爲什麼?
A:get()方法返回一個引用原始對象的對象。

因此,如果寫myArrayList.get(15).itsVariable = 7

myArrayList.get(15).myMethod("My Value")
你實際上分配值/使用來自通過引用的對象的方法返回的一個(這意味着,改變被施加到原始對象)

不能做的唯一的事情就是myArrayList.get(15) = myNewElement。要做到這一點,你必須使用list.set()方法。

+4

這是不正確的。確實,[ArrayList.get()](https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int))返回元素的一個副本,但在元素是一個對象的情況下(就像問題作者的列表一樣),它將引用的副本返回給對象。對該引用所做的任何修改都將反映在列表中,因爲引用指向的內存與列表中的對象完全相同。所以這個:'myList.get(3).setEmail(「[email protected]」);'會在列表的電子郵件的pos 3修改客戶。 – 2016-04-13 15:53:37

+0

@Antoine Dahan是對的。這個答案對象不正確。請修改或刪除它。 – 2016-09-06 14:37:18

0

沒有這裏是函數...它正常工作與充滿對象

例如 `

al.add(new Student(101,"Jack",23,'C'));//adding Student class object 
     al.add(new Student(102,"Evan",21,'A')); 
     al.add(new Student(103,"Berton",25,'B')); 
     al.add(0, new Student(104,"Brian",20,'D')); 
     al.add(0, new Student(105,"Lance",24,'D')); 
     for(int i = 101; i< 101+al.size(); i++) { 
       al.get(i-101).rollno = i;//rollno is 101, 102 , 103, .... 
      }