1ArrayList<String> al = new ArrayList<String>();
2 al.add("Hello");
3 al.add("Wor");
4 al.add("ld");
5 StringBuffer sb = new StringBuffer();
6
7 for (String s : al) {
8 sb.append(s);
9 sb.append(" ");
10 }
11 String str = sb.toString();
12 System.out.println(str);//"Hello wor ld"
1import java.util.ArrayList;
2public class ArrayListExample
3{
4 public static void main(String[] args)
5 {
6 int num = 14;
7 // declaring ArrayList with initial size num
8 ArrayList<Integer> al = new ArrayList<Integer>(num);
9 // append new element at the end of list
10 for(int a = 1; a <= num; a++)
11 {
12 al.add(a);
13 }
14 System.out.println(al);
15 // remove element at index 7
16 al.remove(7);
17 // print ArrayList after deletion
18 System.out.println(al);
19 // print elements one by one
20 for(int a = 0; a < al.size(); a++)
21 {
22 System.out.print(al.get(a) + " ");
23 }
24 }
25}
1//requires either one of these two imports, both work.
2import java.util.ArrayList;
3//--or--
4import java.util.*
5
6ArrayList<DataType> name = new ArrayList<DataType>();
7//Defining an arraylist, substitute "DataType" with an Object
8//such as Integer, Double, String, etc
9//the < > is required
10//replace "name" with your name for the arraylist