Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎


posted Oct 30, 2018, 6:26 AM by Konstantinovich Samuel   [ updated Oct 30, 2018, 8:32 AM ]


AP Subset:

import java.util.ArrayList; import java.util.List; public class ArrayListDemo{ public static void main(String[]args){ List<String> names; //can store any kind of list, but you only //know about ArrayList for now. names = new ArrayList<String>(); names.add("Bart"); names.add(0,"Homer"); names.add("Polyphemus"); names.add("Lisa"); names.add("Charybdis"); names.remove(1); names.remove(2); System.out.println(names); for(int i = 0; i < names.size();i++){ //you cannot use var[i] notation! names.set(i,names.get(i)+"!"); } ArrayList<Integer>nums = new ArrayList<Integer>(); nums.add(new Integer(4));//old way, not used nums.add(3);//automatically convert into an Integer from int nums.add(2);//this is called autoboxing nums.add(Integer.valueOf(1));//proper way System.out.println(nums); nums.remove(1); System.out.println(nums+" after remove(1)"); nums.remove(new Integer(1));//should use valueOf(1) System.out.println(nums+" after remove(new Integer(1))"); } }


Complete the new section with arrayLists

Quiz Next Wed.

PClassic Competition Sign Up:

Remember you can use this to visualize your code... but they dont usually work with ArrayLists.