Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Arrays.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Arrays part 3 Multidimensional arrays, odds & ends.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
11-1 Chapter 11 2D Arrays Asserting Java Rick Mercer.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
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.
©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.
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.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
CSC – Java Programming II Lecture 9 January 30, 2002.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays Part 9 dbg. Arrays An array is a fixed number of contiguous memory locations, all containing data of the same type, identified by one variable.
 Pearson Education, Inc. All rights reserved Arrays.
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.
 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.
Pemrograman Dasar Arrays PTIIK - UB. Arrays  An array is a container object that holds a fixed number of values of a single type.  The length of an.
Java Programming: From Problem Analysis to Program Design, 4e
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.
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.
Lecturer : Sakuni Sellapperuma. Introduction An array is a container object that holds a fixed number of values of a single type. The length of an array.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
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.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
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.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
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.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
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.
Array and String.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
ARRAYS Multidimensional realities Image courtesy of
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
 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.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Java Programming Language Lecture27- An Introduction.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Arrays 4/4 By Pius Nyaanga. Outline Multidimensional Arrays Two-Dimensional Array as an Array of Arrays Using the length Instance Variable Multidimensional.
Java How to Program, Late Objects Version, 10/e
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Multidimensional Arrays
Presentation transcript:

Arrays

Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword

Declaring Arrays  Like all other variables in Java, an array must have a specific type like byte, int, String or double.  Only variables of the appropriate type can be stored in an array. One array cannot store both ints and Strings, for instance.  Like all other variables in Java an array must be declared.

Here are some examples: int[] k; float[] y; String[] names; You also have the option to append the brackets to the variable instead of the type. int k[]; float y[]; However, unlike in C, you cannot include the length of the array in the declaration. The following is a syntax error: int k[3]; float yt[7]; String names[100];

Creating an Array  Arrays are Objects so the new keyword is used  When you instantiate an array you must tell it what size.  Names = new String[100]  K = new int[6]

Referencing Arrays  Individual components of an array are referenced by the array name and by an integer which represents their position in the array. The numbers you use to identify them are called subscripts or indexes into the array.  names[35] k[3]

Initializing Arrays  For example this is how you'd store values in the arrays above: k[0] = 2; k[1] = 5; k[2] = -2; yt[17] = 7.5f; names[4] = "Fred";

 String[] names = {“Fred”, “Alice”, ”Paul”}  For even medium sized arrays, it's unwieldy to specify each component individually. It is often helpful to use for loops to initialize the array. Here is a loop which fills an array with the squares of the numbers from 0 to 100. float[] squares; squares = new float[101]; for (int i=0; i <= 100; i++) { squares[i] = i*i;

ArrayIndexOutOfBoundsException. While we won’t deal with exceptions for a while. You do need to understand this exception. If you attempt to access an index past the size of an array. You will throw this exception.

Multi-Dimensional Arrays  Two dimensional arrays are declared, allocated and initialized much like one dimensional arrays. However you have to specify two dimensions rather than one, and you typically use two nested for loops to fill the array.

This example fills a two-dimensional array with the sum of the row and column indexes class FillArray { public static void main (String args[]) { int[][] matrix; matrix = new int[4][5]; for (int row=0; row < 4; row++) { for (int col=0; col < 5; col++) { matrix[row][col] = row+col; }

Even Higher Dimensions  You don't have to stop with two dimensional arrays. Java permits arrays of three, four or more dimensions. However chances are pretty good that if you need more than three dimensions in an array, you're probably using the wrong data structure. Even three dimensional arrays are exceptionally rare outside of scientific and engineering applications.

 The syntax for three dimensional arrays is a direct extension of that for two-dimensional arrays.

class Fill3DArray { public static void main (String args[]) { int[][][] M; M = new int[4][5][3]; for (int row=0; row < 4; row++) { for (int col=0; col < 5; col++) { for (int ver=0; ver < 3; ver++) { M[row][col][ver] = row+col+ver; } } } }

Unbalanced Arrays  Java does not have true multidimensional arrays. Java fakes multidimensional arrays using arrays of arrays. This means that it is possible to have unbalanced arrays. An unbalanced array is a multidimensional array where the dimension isn't the same for all rows. In most applications this is a horrible idea and should be avoided.

Array methods  arraycopy(Object src, int srcPos, Object dest, int destPos, int length) Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. arraycopyObject src - the source array. srcPos - starting position in the source array. dest - the destination array. destPos - starting position in the destination data. length - the number of array elements to be copied.

public class DoubleArray { public static void main (String args ]) { int array1[] = {1, 2, 3, 4, 5}; int array2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}; System.out.println("Original size: " + array1.length); System.out.println("New size: " + doubleArray(array1).length); System.out.println("Original size: " + array2.length); System.out.println("New size: " + doubleArray(array2).length); } static int[] doubleArray(int original[]) { int length = original.length; int newArray[] = new int[length*2]; System.arraycopy(original, 0, newArray, 0, length); return newArray; } }

getLength()  public static int getLength(Object array) throws IllegalArgumentExceptionObjectIllegalArgumentException Returns the length of the specified array object, as an int. Parameters:  array - the array Returns:  the length of the array Throws:  IllegalArgumentException - if the object argument is not an array IllegalArgumentException

get  public static Object get(Object array, int index) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionObject IllegalArgumentException ArrayIndexOutOfBoundsException Returns the value of the indexed component in the specified array object. The value is automatically wrapped in an object if it has a primitive type. Parameters:  array - the array  index - the index Returns:  the (possibly wrapped) value of the indexed component in the specified array getInt(), getBoolean, getLong(),getDouble()

set  public static void set(Object array, int index, Object value) throws IllegalArgumentException, ArrayIndexOutOfBoundsExceptionObject IllegalArgumentException ArrayIndexOutOfBoundsException Sets the value of the indexed component of the specified array object to the specified new value. The new value is first automatically unwrapped if the array has a primitive component type. Parameters:  array - the array  index - the index into the array  value - the new value of the indexed component  setInt(), setBoolean(), setFloat(),setLong()

Sort - Search  The Arrays class provides many other methods useable with arrays.  Arrays.sort()  Arrays. binarySearch()  Import java.util.*; to use these  To search an array must be sorted.