Comparing Array Lists and Arrays Array Lists Array

  • Slides: 8
Download presentation
Comparing Array. Lists and Arrays

Comparing Array. Lists and Arrays

Array. Lists • Array. Lists are one type of flexible-size collection classes supported by

Array. Lists • Array. Lists are one type of flexible-size collection classes supported by Java – Array. Lists increase and decrease capacity as necessary. • Flexible-size collections such as Array. Lists can only store objects, not primitive data type values • To use Array. Lists, you need to import the Array. List class: import java. util. Array. List; • Declaring and instantiating an Array. List: Array. List<Card> hand; hand = new Array. List<Card>();

Array. Lists • Use the add method to add elements to an Array. List:

Array. Lists • Use the add method to add elements to an Array. List: hand. add(new Card(“Spades”, “Ace”)); • Use the remove method to remove elements from an Array. List: int index = 0; hand. remove(index); • Use the size method of an Array. List to find its size: int index = 0; while(index < hand. size())

Accessing Array. List Elements • Accessing Array. List elements using a for each loop:

Accessing Array. List Elements • Accessing Array. List elements using a for each loop: for(Card card. Object: hand) { System. out. println(card. Object. get. Face()) } • Accessing Array. List elements using the Iterator object and the Iterator object’s has. Next and next methods: Iterator<Card> it = hand. iterator(); while(it. has. Next()) { Card card. Object = it. next(); System. out. println(card. Object. get. Face()); }

Accessing Array. List Elements • Accessing Array. List elements using a while loop, an

Accessing Array. List Elements • Accessing Array. List elements using a while loop, an index and the Array. List get method: int index = 0; while(index < hand. size()) { Card card. Object = hand. get(index); System. out. println(card. Object. get. Face()); index++; } • Accessing Array. List elements using a for loop, an index and the Array. List get method: for(int index = 0; index < hand. size(); index++) { Card card. Object = hand. get(index); System. out. println(card. Object. get. Face()); } • Using no local variable: for(int index = 0; index < hand. size(); index++) { System. out. println(hand. get(index). get. Face()); }

Arrays • Arrays are a named, fixed size, set of variables. • Arrays can

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.

Arrays • Arrays can be created and assigned values in one statement: double[] prices

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, false}; String[] book. Codes = {"warp", "mbdk", "citr"}; Book[] books = {new Book("warp"), new Book("mbdk")}; String[] suits = {"Spades", "Hearts", "Clubs", "Diamonds"};

Arrays • • • Access an array element using the element’s index inside square

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