Solved

How to add an element to an existed list?

Posted on 2006-10-20
2
235 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
ID: 17772780
newList.addAll(oldList);
newList.add(neEle);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17772816
:-)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse digits of a number using for loop 5 53
Why my table column Id is not passed to java object? 4 44
java example issue 5 26
web project error add remove 1 32
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

820 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