Ram N
Posts: 2777
Nickname: ramram
Registered: Jul, 2014
Java : Collection Framework : ArrayList (Add Objects based on Index)
Posted: Oct 2, 2014 1:26 AM
This post originated from an RSS feed registered with Java Buzz
by Ram N.
Original Post: Java : Collection Framework : ArrayList (Add Objects based on Index)
Feed Title: JAVA EE
Feed URL: http://ramj2ee.blogspot.com/feeds/posts/default?alt=rss
Feed Description: This blog has viedo tutorials and Sample codes related to below Technologies.
1.J2EE
2.Java
3.Spring
4.Hibernate
5.Database
6.Oracle
7.Mysql
8.Design Patterns
Latest Java Buzz Posts
Latest Java Buzz Posts by Ram N
Latest Posts From JAVA EE
Advertisement
import java.util.ArrayList;/* * Example of add(int index, E element) method */ public class ArrayListAddIndexBasedExample { public static void main (String[] args ) { ArrayList<String> arrayList = new ArrayList<String>(); arrayList. add("Ram" ); arrayList. add("Dave" ); arrayList. add("Peter" ); System. out. println("ArrayList : " + arrayList); /* * In Position 2 adding "Stephan" */ arrayList. add(2 , "Stephan" ); System. out. println("ArrayList : " + arrayList); } }Output ArrayList : [Ram, Dave, Peter] ArrayList : [Ram, Dave, Stephan, Peter] ArrayListAddAllIndexBasedExample.java import java.util.ArrayList;import java.util.HashSet;import java.util.List;import java.util.Set;/* * Example of addAll(int index, Collection<? extends E> c) method */ public class ArrayListAddAllIndexBasedExample { public static void main (String[] args ) { List<String> list = new ArrayList<String>(); list. add("Ram" ); list. add("Dave" ); list. add("Peter" ); System. out. println("List : " + list); Set<String> set = new HashSet<String>(); set. add("Julia" ); set. add("Akram" ); System. out. println("Set : " + set); /* * In Position 1 adding Set collection elements to the list collection */ list. addAll(1 , set); System. out. println("List : " + list); } }Output List : [Ram, Dave, Peter] Set : [Julia, Akram] List : [Ram, Julia, Akram, Dave, Peter] To Download ArrayListDemoAddIndex Project Click the below link https://sites.google.com/site/javaee4321/java-collections/ArrayListDemoAddIndex.zip?attredirects=0&d=1 See also: All JavaEE Viedos Playlist All JavaEE Viedos Servlets Tutorial All Design Patterns Links JDBC Tutorial
Read: Java : Collection Framework : ArrayList (Add Objects based on Index)