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

2018-10-30

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

ArrayList:
https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html

AP Subset:
https://www2.cs.duke.edu/csed/ap/subset/doc/ap/java/util/ArrayList.html


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))"); } }

Classwork/Homework:
https://codingbat.com/home/konstans@stuy.edu/apcs1

Complete the new section with arrayLists

Quiz Next Wed.

PClassic Competition Sign Up:
http://bert.stuy.edu/pbrooks/pclassic/pclassic-fall-2018.html


Remember you can use this to visualize your code... but they dont usually work with ArrayLists.
https://cscircles.cemc.uwaterloo.ca/java_visualize/


Comments