70 likes | 181 Views
ArrayLists. Arrays fixed size Declare exactly how many you want ArrayList Dynamic Variable length Can grow and shrink Generic class. ArrayList <E>. E – element ArrayList <String> ArrayList <Point>. Create an empty ArrayList. import java.util .*; public class ArrayL {
E N D
ArrayLists • Arrays • fixed size • Declare exactly how many you want • ArrayList • Dynamic • Variable length • Can grow and shrink • Generic class
ArrayList<E> • E – element • ArrayList<String> • ArrayList<Point>
Create an empty ArrayList import java.util.*; public class ArrayL { static Scanner console = new Scanner(System.in); public static void main (String args[]) { ArrayList<String> list = new ArrayList<String>(); System.out.println("List = " + list); } } > run ArrayL List = []
Basic ArrayList operations import java.util.*; public class ArrayL { static Scanner console = new Scanner(System.in); public static void main (String args[]) { ArrayList<String> list = new ArrayList<String>(); list.add("Apple"); //new values are added to the end list.add("Orange"); System.out.println("List = " + list); } }
import java.util.*; public class ArrayL { static Scanner console = new Scanner(System.in); public static void main (String args[]) { ArrayList<String> list = new ArrayList<String>(); System.out.println("List = " + list); list.add("Apple"); list.add("Orange"); System.out.println("List = " + list); list.add(1,"Broccoli"); System.out.println("List = " + list); } } List = [] List = [Apple, Orange] List = [Apple, Broccoli, Orange]