Solved

How to add an element to an existed list?

Posted on 2006-10-20
2
209 Views
Last Modified: 2010-03-31
Assumed I have an existed list, how can I add an element to it at run time? I do as follow:

...
   List newList = new ArrayList();
   List oldList = myObject.getList();
   MyElement newEle = new MyElement();
   for(int i=0; i<oldList.size(); i++)
      newList.add(oldList.get(i));
   newList.add(neEle);
...

I think this way seems not so elegent. Someone has any better idea?
0
Comment
Question by:chenwei
  • 2
2 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 30 total points
Comment Utility
newList.addAll(oldList);
newList.add(neEle);
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now