2012-04-07 180 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, .... 
      }