16
ArrayLists

ArrayLists' An array is a variable type that represents a list of items. You access individual items in an array by index. ' Store a single type of item (int, double, GRect, etc.)

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

ArrayLists

Source:TheHobbit

Previously…

Source:TheHobbit

After this lecture!

LearningGoals1. KnowhowtostoredatainandretrievedatafromanArrayList

• Avariabletypethatrepresentsalistofitems.

• Youaccessindividualitemsbyindex.

• Storeasingletypeofobject(String,GRect,etc.)

• Resizable – canaddandremoveelements

• Hashelpfulmethodsforsearchingforitems

Meet ArrayLists

Wow! Nice to meet you!

//Createan(initiallyempty)listArrayList <Integer>list=new ArrayList<Integer>();

//Addanelementtothebacklist.add(16);//nowsize1list.add(42);//nowsize2

ArrayList

//Createan(initiallyempty)listArrayList <Integer>list=new ArrayList<Integer>();

//Addanelementtothebacklist.add(16);//nowsize1list.add(42);//nowsize2

//Accesselementsbyindex(startingat0!)println(list.get(0));//prints16println(list.get(1));//prints42

ArrayList

//Accesselementsbyindex(startingat0!)for(int i =0;i <list.size();i++){println(list.get(i));

}

ArrayList

ArrayList Methods

Insert/Remove

Example

Rocket Paddle

ArrayLists and Primitives

ArrayLists and Primitives

ArrayLists and Primitives

ArrayLists vs. Arrays