Solved

How to replace an element in an existed list?

Posted on 2006-10-20
2
218 Views
Last Modified: 2010-03-31
Assumed a list has 3 elements and I want to replace the second one at run-time. I do as follow:

...
   List newList = new ArrayList();
   List oldList = myObject.getList();
   MyElement newEle = new MyElement();
   ...
   oldList.remove(1);
   for(int i=0; i<oldList.size; i++){
      if(i!=1)
        newList.add(oldList.get(i));
      else
        newList.add(myEle);
   }
....

I find it's quite complecated. Someone has any other better way?
     
0
Comment
Question by:chenwei
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
ID: 17772916
oldList.set(1, myEle);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17772955
:-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question