Download presentation
Presentation is loading. Please wait.
Published byBrenda Anthony Modified over 9 years ago
1
Comparing ArrayLists and Arrays
2
ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease capacity as necessary. Flexible-size collections such as ArrayLists can only store objects, not primitive data type values To use ArrayLists, you need to import the ArrayList class: import java.util.ArrayList; Declaring and instantiating an ArrayList: ArrayList hand; hand = new ArrayList ();
3
ArrayLists Use the add method to add elements to an ArrayList: hand.add(new Card(“Spades”, “Ace”)); Use the remove method to remove elements from an ArrayList: int index = 0; hand.remove(index); Use the size method of an ArrayList to find its size: int index = 0; while(index < hand.size())
4
Accessing ArrayList Elements Accessing ArrayList elements using a for each loop: for(Card cardObject: hand) { System.out.println(cardObject.getFace()) } Accessing ArrayList elements using the Iterator object and the Iterator object’s hasNext and next methods: Iterator it = hand.iterator(); while(it.hasNext()) { Card cardObject = it.next(); System.out.println(cardObject.getFace()); }
5
Accessing ArrayList Elements Accessing ArrayList elements using a while loop, an index and the ArrayList get method: int index = 0; while(index < hand.size()) { Card cardObject = hand.get(index); System.out.println(cardObject.getFace()); index++; } Accessing ArrayList elements using a for loop, an index and the ArrayList get method: for(int index = 0; index < hand.size(); index++) { Card cardObject = hand.get(index); System.out.println(cardObject.getFace()); } Using no local variable: for(int index = 0; index < hand.size(); index++) { System.out.println(hand.get(index).getFace()); }
6
Arrays Arrays are a named, fixed size, set of variables. Arrays can store objects or primitive-type values. Declaring and instantiating an Array object: Card[] deck; deck = new Card[52]; boolean[] available = new boolean[52]; Notice that when an Array is declared you know what type of data it will hold. When the Array is instantiated, the size is set.
7
Arrays Arrays can be created and assigned values in one statement: double[] prices = {14.95, 12.95, 11.95, 9.95}; int[] values = {3, 5, 7, 9}; boolean[] responses = {true, false, true, true, false}; String[] bookCodes = {"warp", "mbdk", "citr"}; Book[] books = {new Book("warp"), new Book("mbdk")}; String[] suits = {"Spades","Hearts","Clubs","Diamonds"};
8
Arrays Access an array element using the element’s index inside square brackets Assign values to Array elements The Array length field contains the size of the array. boolean[] available = new boolean[50]; for(int index = 0; index < available.length; index++) { available[index] = true; } String[] suits = {"Spades", "Hearts", "Clubs", "Diamonds"}; String[] faces = {"Ace","2","3","4","5","6","7","8","9","10", "Jack","Queen","King"}; Card[] deck = new Card[52]; for(int index = 0; index < deck.length; index++) { deck[index] = new Card(suits[index/13], faces[index%13]); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.