Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Java Unit 9: Arrays Declaring and Processing Arrays.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 8: Arrays.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Liang, Introduction to Java Programming1 Arrays Gang Qian Department of Computer Science University of Central Oklahoma.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 6 Arrays.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Introducing Arrays. Too Many Variables?  Remember, a variable is a data structure that can hold a single value at any given time.  What if I want to.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 7 Single-Dimensional Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Single Dimensional Arrays
Chapter 7: Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 7 Single-Dimensional Arrays
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Chapter 6 Arrays.
Chapter 7 Arrays Data structures Related data items of same type
Chapter 6 Arrays Solution Opening Problem
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
METHODS (FUNCTIONS) By: Lohani Adeeb khan.
Object Oriented Programming in java
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Chapter 6 Arrays.
Single-Dimensional Arrays
Arrays in Java.
Chapter 6 Arrays.
CSS161: Fundamentals of Computing
Chapter 7 Single-Dimensional Arrays
Chapter 7 Single-Dimensional Arrays
Corresponds with Chapter 5
Presentation transcript:

Arrays Chapter 8

What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1 = 85; int score2 = 92; int score3 = 90; int score4 = 74; int score5 = 83; Or we could store the values in an array Test scores

 An array is a data structure used to process a collection of data that is all of the same type  An array behaves like a numbered list of variables with a uniform naming mechanism  It has a part that does not change: the name of the array  It has a part that can change: an integer in square brackets  For example, given five scores: score[0], score[1], score[2], score[3], score[4]

 An array that behaves like this collection of variables, all of type integer, can be created using one statement as follows: int[] score = new int[5];  Or using two statements: int[] score; score = new int[5];  The first statement declares the variable score to be of the array type int[]  The second statement creates an array with five numbered variables of type int and makes the variable score a name for the array

 The individual variables that together make up the array are called indexed variables  They can also be called subscripted variables or elements of the array  The number in square brackets is called an index or subscript  In Java, indices must be numbered starting with 0, and nothing else score[0], score[1], score[2], score[3], score[4]

 The number of indexed variables in an array is called the length or size of the array  When an array is created, the length of the array is given in square brackets after the array type  The indexed variables are then numbered starting with 0, and ending with the integer that is one less than the length of the array score[0], score[1], score[2], score[3], score[4]

 Test scores array score indices

 An array can be initialized when it is declared  Values for the indexed variables are enclosed in braces, and separated by commas  The array size is automatically set to the number of values in the braces int[] score = {85, 92, 90, 74, 83};  Using this shorthand notation, you have to declare, create, and initialize the array all in one statement. Using the two statement method would cause a syntax error

 Another way of initializing an array is by using a for loop: int[] score = new int[5]; int index; for (index = 0; index < score.length; index++) score[index] = 85;  If the elements of an array are not initialized explicitly, they will automatically be initialized to the default value for their base type

public class Test { public static void main(String[ ] args) { int[ ] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; }  What is created with the following code?

Declare array variable values, create an array, and assign its reference to values public class Test { public static void main(String[ ] args) { int[ ] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } value[0] value[1] value[2] value[3] value[4] i = 1:value[1] is 1 + value[0] = = 1 i = 2:value[2] is 2 + value[1] = = 3 i = 3:value[3] is 3 + value[2] = = 6 i = 4:value[4] is 4 + value[3] = = value[0] is value[1] + value[4] = = 11 11

Scanner input = new Scanner(System.in); System.out.print("Enter 10 values: "); for (int i = 0; i < 10; i++) myList[i] = input.nextDouble();  How do we input arrays through a scanner?

 Strings can be part of an array  Create array of Strings with 3 elements String[] deptName = {"Accounting", "Human Resources", "Sales"}; for(int a = 0; a < deptName.length; ++a) System.out.println(deptName[a]);

for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " "); }  To print the entire array, create a loop to print each element

double total = 0; for (int i = 0; i < myList.length; i++) { total = total + myList[i]; }

double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; }

 To search for a specific value, simply loop through the loop  Look at each entry and compare it with the specified value boolean found = false; int i = 0; while (i < myList.length && !found) { if (searchEntry == myList[i]) found = true; i++; } if (found) System.out.println(searchEntry + " occurs in array");

 Both array indexed variables and entire arrays can be used as arguments to methods  An indexed variable can be an argument to a method in exactly the same way that any variable of the array base type can be an argument

Java uses pass by value to pass arguments to a method. There are important differences between passing a value of variables of primitive data types and passing arrays.  For a parameter of a primitive type value, the actual value is passed. Changing the value of the local parameter inside the method does not affect the value of the variable outside the method.  For a parameter of an array type, the value of the parameter contains a reference to an array; this reference is passed to the method. Any changes to the array that occur inside the method body will affect the original array that was passed as the argument.

public class Test { public static void main(String[] args) { int[] scores = {85, 92, 90, 74, 83}; myMethod(scores); System.out.println("scores[0] is " + scores[0]); } public static void myMethod(int[] numbers) { numbers[0] = 5555; // Assign a new value to numbers[0] }

public static int[ ] reverse(int[ ] list) { int[ ] result = new int[list.length]; for (int i = 0, j = result.length - 1; i < list.length; i++, j--) { result[j] = list[i]; } return result; }  Here is a method that returns the array in reverse order