Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Two Dimensional Arrays and ArrayList
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Chapter Seven: Arrays and Array Lists.
Chapter 7 Arrays and Array Lists. Chapter Goals To become familiar with using arrays and array lists To learn about wrapper classes, auto-boxing and the.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 17 – Generic Programming.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Chapter 18 – Generic Classes.
Chapter 8 – Interfaces and Polymorphism Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 7 – Arrays.
Computer Science A 10: 20/3. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[]
Array Lists Chapter 7.2 Pages Array Lists In Java, Arrays are an important structure for storing data. We will learn more about arrays later,
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
Java Programming Week 6: Array and ArrayList Chapter 7.
For each primitive type there is a wrapper class for storing values of that type: Double d = new Double(29.95); Wrapper Classes Wrapper objects can be.
Week 9-10 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
ICOM 4015: Advanced Programming Lecture 7 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Reading: Chapter Seven:
Chapter Goals To collect elements using arrays and array lists
Week 10 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved For Loops October 16, 2013 Slides by Evan Gallagher.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. if (amount
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Data collections Android Club Agenda Array ArrayList HashMap.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Fall 2006Slides adapted from Java Concepts companion slides1 Arrays and Array Lists Advanced Programming ICOM 4015 Lecture 7 Reading: Java Concepts Chapter.
COP INTERMEDIATE JAVA Data Structures. A data structure is a way of organizing a collection of data so that it can be manipulated effectively. A.
Arrays…JavaCPython have fixed lengthyes*yesno are initialized to default values yesno? track their own lengthyesnoyes trying to access “out of bounds”
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Chapter 6 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
COMP 121 Week 8: Generic Collections. Objectives To understand type variables and how they are used in generic programming To be able to implement and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Copyright © 2013 by John Wiley & Sons. All rights reserved. ARRAYS and ARRAYLISTS CHAPTER Slides by Donald W. Smith TechNeTrain.com 6 Final Draft 10/30/2011.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
COM S 207 ArrayList Instructor: Ying Cai Department of Computer Science Iowa State University
Array length = maximum number of elements in array Usually, array is partially filled Need companion variable to keep track of current size Uniform naming.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Arrays. Array: Sequence of values of the same type Construct array: Store in variable of type double[ ] new double[10] double[] data = new double[10];
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 1.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Arrays and Array Lists CS 21a. Problem 1: Reversing Input Problem: Read in three numbers and then print out the numbers in reverse order Straightforward.
The ArrayList Data Structure The Most Important Things to Review.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter Three - Implementing Classes.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. A string is a sequence of characters Strings are objects of the String.
Week 9 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 6 – Array and Array Lists Two-Dimensional Arrays
Chapter 7 – Arrays and Array Lists
Array Lists 7.7.
Array Lists An array list stores a sequence of values whose size can change. An array list can grow and shrink as needed. ArrayList class supplies methods.
Chapter 7 – Arrays and Array Lists
Chapter 8 – Arrays and Array Lists
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
7.6 Common Array Algorithm: Filling
Intro to Collections.
Chapter Three - Implementing Classes
Object Oriented Programming in java
ArrayLists 22-Feb-19.
Use a variable to store a value that you want to use at a later time
Chapter 5 – Decisions Big Java by Cay Horstmann
Presentation transcript:

Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array is created, all values are initialized depending on array type: Numbers: 0 Boolean : false Object References: null Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. double[] values = new double[10];

Arrays Use [] to access an element: values[2] = 29.95; Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Using the value stored: System.out.println("The value of this data item is " + values[2]); Get array length as values.length (Not a method!) Index values range from 0 to length - 1 Accessing a nonexistent element results in a bounds error: double[] values = new double[10]; values[10] = 29.95; // ERROR Limitation: Arrays have fixed length Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Declaring Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

Syntax 7.1 Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

// Don't do this int[] accountNumbers; double[] balances; Make Parallel Arrays into Arrays of Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 7

Avoid parallel arrays by changing them into arrays of objects: BankAccount[] accounts = new BankAccount[10]; Make Parallel Arrays into Arrays of Objects Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 8

An Early Internet Worm Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 9

Array Lists ArrayList class manages a sequence of objects Can grow and shrink as needed ArrayList class supplies methods for many common tasks, such as inserting and removing elements ArrayList is a generic class: ArrayList collects objects of type parameter T : ArrayList names = new ArrayList (); names.add("Emily"); names.add("Bob"); names.add("Cindy"); size method yields number of elements Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 10

To add an object to the end of the array list, use the add method: names.add("Emily"); names.add("Bob"); names.add("Cindy"); Adding Elements Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

To obtain the value an element at an index, use the get method Index starts at 0 String name = names.get(2); // gets the third element of the array list Bounds error if index is out of range Most common bounds error: int i = names.size(); name = names.get(i); // Off-by-one error // legal index values are 0... i-1 Retrieving Array List Elements Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

To set an element to a new value, use the set method: names.set(2, " Carolyn " ); Setting Elements Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

To remove an element at an index, use the remove method: names.remove(1); Removing Elements Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.

names.add("Emily"); names.add("Bob"); names.add("Cindy"); names.set(2, "Carolyn"); names.add(1, "Ann"); names.remove(1); Adding and Removing Elements Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 15

Working with Array Lists ArrayList names = new ArrayList (); Constructs an empty array list that can hold strings. names.add("Ann"); names.add("Cindy"); Adds elements to the end. System.out.println(names); Prints [Ann, Cindy]. names.add(1, "Bob"); Inserts an element at index 1. names is now [Ann, Bob, Cindy]. names.remove(0); Removes the element at index 0. names is now [Bob, Cindy]. names.set(0, "Bill"); Replaces an element with a different value. names is now [Bill, Cindy]. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 16

Working with Array Lists (cont.) String name = names.get(i); Gets an element. String last = names.get(names.size() - 1); Gets the last element. ArrayList squares = new ArrayList (); for (int i = 0; i < 10; i++) { squares.add(i * i); } Constructs an array list holding the first ten squares. Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 17

18 Syntax 7.2 Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. 18