View RSS Feed

Algorithm and Data Structure

Adding Elements to a List

Rate this Entry
by , 02-19-2012 at 06:47 AM (420 Views)
Elements are added to list by using these 4 different methods given below:
Java Code:
MyList.add("1");
//or
MyList.add(varName);
An item is added by it to end of list.
At a particular location, elements could be added as given below.
Java Code:
MyList.add(0, "2");
//or
MyList.add(0, varName);
Zero tells the element location, as given in above code. This is basically added as 1st element.
Other two ways of adding are self documented. They are addLast() & addFirst() functions. Just like add() methods, they are used.
Java Code:
MyList.addFirst("3");
MyList.addLast("4");

Submit "Adding Elements to a List" to Facebook Submit "Adding Elements to a List" to Digg Submit "Adding Elements to a List" to del.icio.us Submit "Adding Elements to a List" to StumbleUpon Submit "Adding Elements to a List" to Google

Categories
Linked List

Comments