arraylists' an array is a variable type that represents a list of items. you access individual...

16
ArrayLists

Upload: others

Post on 22-May-2020

11 views

Category:

Documents


0 download

TRANSCRIPT

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