Java Unit 9: Arrays Declaring and Processing Arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
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.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
CS0007: Introduction to Computer Programming Introduction to Arrays.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
4.1 Instance Variables, Constructors, and Methods.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 8 Arrays and Strings
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
ArrayList, Multidimensional Arrays
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
1 Object-Oriented Programming (Java), Unit 9 (comes before Unit 20) Kirk Scott.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
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.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Chapter 8: Arrays Introduction to arrays Declaring arrays Initializing arrays Examples using arrays Relationship with pointers Array passing to a function.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Multidimensional Arrays Computer and Programming.
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.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ARRAYS Multidimensional realities Image courtesy of
JAVA Programming (Session 2) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Arrays Chapter 7.
Computer Programming BCT 1113
JavaScript: Functions.
Chapter 6 Arrays.
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.
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
CIS16 Application Development and Programming using Visual Basic.net
7 Arrays.
Arrays in Java.
Arrays Arrays A few types Structures of related data items
C++ Array 1.
Arrays.
Presentation transcript:

Java Unit 9: Arrays Declaring and Processing Arrays

What are arrays? Arrays are ordered collections of data items that are all of the same type. Using subscript notation an array with the name a would take this form: (a 0, a 1, …, a i, …, a n-1 )

What are arrays? Each element of the array could contain a value, such as a double. The elements are identified by the order in which they appear in the array. In computer science, it is customary to start with 0. Element #:0123 Value:

Constructing arrays Arrays in Java are objects, constructed like this: double[] myArray; myArray = new double[10]; In this declaration, ‘double’ is the type of value that all of the elements in the array will be. The brackets after ‘double’ indicates that this variable is an array. In the assignment, we use the keyword new, followed by the type with brackets. In the brackets, the number of elements in the array is defined. In this case, it’s 10. You could also say the array has a size of 10.

Constructing arrays As usual, you can declare and assign in a single line. double[] myArray = new double[10];

Accessing arrays Once an array has been constructed, it’s elements can be accessed using an integer index value in square brackets. The element of the array, a subscripted variable, can be used like any other variable that is the same type as the array. myArray[0] = 25.2; double myVar = myArray[0]; System.out.println(myArray[0]);

Array indexing Arrays start at the 0 th element. So if an array of size 10 is declared, it’s indexing runs from 0 to 9. Ignoring element 0 may or may not be a mistake, but you can not access myArray[10], because it goes outside the array. It’s similar to how a string object indexes its characters HELLO

Array Types It is possible to declare arrays containing simple types and object references: int[], double[], boolean[], String[] Although local variables do not have initialization in general, array contents have default initialization regardless of where they are declared or how they are used. Numerics are 0 by default Booleans are false by default Object references are null by default

main(String[] args) Consider this line of code that appears in every program: public static void main(String[] args) We can see that it takes an array of Strings as a parameter. If this program is run from the command line, multiple command line arguments can be entered on the same line that executes the program, with each argument separated by a string: java MyJava.class myArg1 myArg2 myArg3 The command line arguments would be stored inside an array, and passed into the main() method as ‘args’.

Arrays and Objects An array can be declared to contain objects of any kind of object. Cup[] myCups = new Cup[10]; //An array of 10 Cup object references As usual, the class name is capitalized. The name of the array is a reference to a constructed object, so it is not capitalized. If we index a single element in the object array, we can call methods on them like any other object. int myCount = myCups[3].getSeedCount();

Arrays and Loops Loops are incredibly useful in processing arrays. Since the elements of arrays can be identified by consecutive integers, it is a simple matter to use the loop index as the array index: for(int j = 0; j < 10; j++) { myArray[j] = j * j; } The initial value of j is 0, the beginning element of the array. The last value of j is 9, so it stops before it refers to a non-existent element. We could have written the looping condition as j <= 9, but there is a logic to using strict inequality and the size of the array, explained on the next slide.

Arrays and Loops Because the size of arrays is needed so often, Java has provided the means to obtain it: myArray.length This returns the number of elements for that array Note that in this case there is no parenthesis, nor is there a method called getLength() ‘length’ is actually a ‘public final’ instance variable. We can access this variable because it is public. Using the size of the array itself instead of a hard-coded value, processing the entire array becomes this: for(int j = 0; j < myArray.length; j++) { myArray[j] = j * j; }

Arrays and Loops Arrays can be traversed in loops in either direction. To access the array from the last element to first, the starting and stopping variables of the index have to be set accordingly. for(int j = myArray.length – 1; j >= 0; j--) {…}

Arrays are fixed in size It is important to note that just like with any other data item, an array can only be declared once. In that one declaration, its size, the number of elements in the array is fixed. The size cannot be changed later on during the course of a program run. This makes it necessary to anticipate the largest number of elements that might ever be needed and declare the size of the array accordingly. This may mean that a large number of positions in the array will remain unused.

Multi-dimensional arrays Arrays of more than one dimension can be declared. Two dimensional arrays, or matrices, can be represented as a table of values, which is m x n in size. For both dimensions, indexing starts at 0 as usual. This means that indexing goes up to m – 1 for the rows, and n – 1 for columns. N, MN = 0N = 1N = 2N = 3 M = 00, 01, 02, 03, 0 M = 10, 11, 12, 13, 1 M = 20, 21, 22, 23, 2

Multi-dimensional arrays The first subscript of an element represents its row and the second represents its column. The declaration of such an in Java, if it contained integer elements and 4 rows and 5 columns, would take this form: int[][] myIntArray = new int[4][5]; The parts of the declaration of this array are analogous to the corresponding parts for a one dimensional array. The only difference is that two sets of square brackets are used, the first indicating the rows, and the second the columns.

Accessing Multi-dimensional arrays Individual elements are accessed by specifying the index values for the row and column in square brackets. For example: myIntArray[2][4] = 95; Two dimensional arrays can be processed conveniently using nested for loops. Just as with one dimensional arrays, array indexing starts at 0 and the lengths of the dimensions can be used to set limits on the loop indexes. This introduces a slight complexity.

Accessing Multi-dimensional arrays for(int i = 0; i < myIntArray.length; i++) { for(int j = 0; j < myIntArray[0].length; j++) { … } } myIntArray.length gives the number of rows in myIntArray myIntArray[0].length gives the number of columns in the 0 th row (or any row) of myIntArray

Accessing Multi-dimensional arrays A two dimensional array is really a one dimensional array of references to a set of one dimensional arrays. In other words, it’s an array of arrays. The value of the length returned for the name of the array alone is the number of rows, or the number of one dimensional arrays it contains. If the name of a two dimensional array is given with a single set of brackets, this represents a single row. The length of one row is the number of columns in the two dimensional array. Syntactically, the use of square brackets to specify the dimensions is analogous to the two dimensional case. The underlying reality is that a three dimensional array is an array of arrays of arrays, and so on for higher dimensions.