Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Programming with Collections Collections in Java Using Arrays Week 9.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
1 Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved 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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
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.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Section 5 - Arrays. Problem solving often requires information be viewed as a “list” List may be one-dimensional or multidimensional List is implemented.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
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.
Arrays in java Unit-1 Introduction to Java. Array There are situations where we might wish to store a group of similar type of values in a variable. Array.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
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.
Arrays Chapter 7.
Computer Programming BCT 1113
Array, Strings and Vectors
JavaScript: Functions.
Chapter 6 Arrays.
Java How to Program, Late Objects Version, 10/e
Chapter 6 Arrays Solution Opening Problem
Chapter 5 Arrays Introducing Arrays
Introduction To Programming Information Technology , 1’st Semester
Java Arrays & Strings.
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
Chapter 6 Arrays.
Single-Dimensional Arrays chapter6
C++ Array 1.
Presentation transcript:

Introduction to Arrays in Java Corresponds with Chapter 6 of textbook

What is an Array? Array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects. This means array variables are references, not value variable. Individual data items in arrays are called elements. Elements are stored consecutively (contiguously) in memory. Each element of an array is indexed.Indexing begins at 0.

Introducing Arrays Array is a data structure that represents a collection of the same types of data.

Declaring Arrays datatype[] arrayname; datatype[] arrayname;Example: int[] myList; datatype arrayname[]; datatype arrayname[];Example: int myList[]; Either syntax will work. The [] indicates that the variable being declared will be a reference to an array. Each element of the array will be a data item of the specified data type. NOTE: declaring the array does not create it! No memory is allocated for individual array elements. This requires a separate creation step.

Creating Arrays arrayName = new datatype[arraySize]; NOTE: the new keyword creates an object or array. The object or array is created in a location of memory called the heap. A reference (pointer) to the array is assigned (via =) to the variable. Example: myList = new double[10]; Java reserved word for object creation Integer value indicates the number of elements

Declaring and Creating in One Step datatype[] arrayname = new datatype[] arrayname = new datatype[arraySize]; datatype[arraySize]; double[] myList = new double[10]; double[] myList = new double[10]; datatype arrayname[] = new datatype[arraySize]; datatype arrayname[] = new datatype[arraySize]; double myList[] = new double[10]; NOTE: when creating an array, the value inside the square brackets [ ] indicates the size you want the array to be (i.e. the number of elements).

The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length For example, myList.length returns 10

Initializing Arrays Using a loop: Using a loop: for (int i = 0; i < myList.length; i++) myList[i] = someValue; Declaring, creating, initializing in one step: Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; NOTE: when using an array, the value inside the square brackets [ ] indicates the index that you are looking at (i.e. the specific element).

Declaring, creating, initializing Using the Shorthand Notation double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5;

Listing 6.1 – TestArray Class 1)Declaring the reference variable 2)Creating the array 3)Assigning the array’s memory location to the reference variable 4)NOTE: TOTAL_NUMBERS is a constant with value 6 Note: new is an operator that creates an object (an array is an example of an object).

How Variables are Kept in Memory Two categories of variables Two categories of variables Value – contains actual data (integer, float, double, boolean, or other primitive data type) Value – contains actual data (integer, float, double, boolean, or other primitive data type) Reference – contains a reference (pointer) to an object or array that is located on the heap (as a result of using the new operator). Reference – contains a reference (pointer) to an object or array that is located on the heap (as a result of using the new operator). Two different locations of memory (more to come later) Two different locations of memory (more to come later) Frame Stack – contains local variables and parameters of methods Frame Stack – contains local variables and parameters of methods Heap – contains objects and arrays Heap – contains objects and arrays The new keyword always creates an object or array and places it in the heap. The new keyword always creates an object or array and places it in the heap.

Heap Frame Stack main’s frame args 1)Declaring the reference variable numbers 2) Creating the array on the heap ) Assigning the array’s memory location to the reference variable

Listing 6.1 – TestArray Class In a loop, read user input and insert into the array. Note use of length property to decide when to end loop

Assume the user enters the numbers: 3, 2, 7, 5, 7, 1 Heap Frame Stack main’s frame args numbers i Looping through the array Note the use of the loop counter to index into the array length is a property of array objects, indicating the number of elements

Listing 6.1 – TestArray Class This loop is a useful technique for finding the largest value in an array. Note the if statement nested in the for loop. Note the use of the loop counter to index into the array.

Heap Frame Stack main’s frame args numbers i 1 Looping through the array max

Listing 6.1 – TestArray Class This use of a loop with an array is similar to a linear search. More on search routines later.

Listing 6.1 – TestArray Class Note the concatenation of array elements to a string, again, repetitively in a loop.

Parallel Arrays Parallel arrays should have same length Same index position  same entity

Program output:

M ultidimensional Array s A multidimensional array is a data structure that represents a collection of the same types of data. Java treats these arrays as objects. This means array variables are references, not value variable. Individual data items in arrays are called elements. Elements are stored consecutively (contiguously) in memory. Can vary in size from two to n. Element [0][0]Element [0][1] Element [1][0] Element [3][1] Element [2][1] Element [1][1] Element [3][0] Element [2][0]

Declaring Multidimensional Arrays datatype[][] arrayname; datatype[][] arrayname;Example: int[][] myList; datatype arrayname[][]; datatype arrayname[][];Example: int myList[][]; Either syntax will work. The [][] indicates that the variable being declared will be a reference to an array. Each element of the array will be a data item of the specified data type. NOTE: declaring the array does not create it! No memory is allocated for individual array elements. This requires a separate creation step.

Creating Multidimensional Arrays arrayName = new datatype[rowSize][columnSize]; NOTE: the new keyword creates an object or array. The object or array is created in a location of memory called the heap. A reference (pointer) to the array is assigned (via =) to the variable. Example: myList = new double[10][4]; Java reserved word for object creation Integer value indicates the number of columns Integer value indicates the number of rows

Declaring and Creating in One Step datatype[][] arrayname = new datatype[][] arrayname = new datatype[rowSize][columnsize]; datatype[rowSize][columnsize]; double[][] myList = new double[10][3]; double[][] myList = new double[10][3]; datatype arrayname[][] = new datatype[rowSize][columnSize]; datatype arrayname[][] = new datatype[rowSize][columnSize]; double myList[][] = new double[10][3]; NOTE: when creating a multidimensional array, the value inside the square brackets [ ] [ ] indicates the number of rows and columns. The total size of the array is the rows multiplied by the columns (10 * 3 = 30 elements for the example above).

Initializing 2-Dimensional Arrays for (int r =0; r<row; r++) { for (int c = 0; c < column; c++){ myList[r][c]=someValue; } double[][] myList = { {1.9, 2.9}, {1.9, 2.9}, {3.4, 3.5} {3.4, 3.5}}; NOTE: when using an array, the values inside the square brackets [] [] indicate the indexes that you are looking at (i.e. the specific element). A 2-D array is an array of arrays Use a nested loop to access all elements of a 2-D array, loop counters represent row and column indexes to the array.

Obtaining Lengths of Multidimensional Arrays for (int r=0; r<myList.length; r++){ for (int c = 0; c<myList[r].length; c++) { print(myList[r][c]); } NOTE: The length determines the number of elements myList.length determines the number of rows myList[r].length determines the number of elements for a given row

Ragged Arrays When rows in a 2-d array can vary in size Int [] [] myList = { {1, 2, 3}, {1, 2}, {1}};

Listing 6.12 – TotalScore Class 1)Declaring the reference variable 2)Creating the 3-d array d1 = student (7) d2 = exam (5) d3 = exam part (2) 3)Assigning the array’s memory location to the reference variable

How Variables are Kept in Memory Two categories of variables Two categories of variables Value – contains actual data (integer, float, double, boolean, or other primitive data type) Value – contains actual data (integer, float, double, boolean, or other primitive data type) Reference – contains a reference (pointer) to an object or array that is located on the heap (as a result of using the new operator). Reference – contains a reference (pointer) to an object or array that is located on the heap (as a result of using the new operator). Two different locations of memory (more to come later) Two different locations of memory (more to come later) Frame Stack – contains local variables and parameters of methods Frame Stack – contains local variables and parameters of methods Heap – contains objects and arrays Heap – contains objects and arrays The new keyword always creates an object or array and places it in the heap. The new keyword always creates an object or array and places it in the heap.

Heap Frame Stack main’s frame args 1)Declaring the reference variable scores 3) Assigning the array’s memory location to the reference variable … [0,0,0] [0,0,1][0,1,0] [0,1,1] … Note: 7 rows, 5 columns with 2 parts each for a total of 70 elements – only 4 are shown

Listing 6.12 – TotalScore Class In a loop, total all of the exam parts together. Display the total for each student. Note: 3-D array  loop nested 3-deep.

Program output:

Contents of memory at breakpoint Note: a 2- dimensional array is implemented as an array of arrays. Each element of the first dimension is a reference to a single dimensional array.