Arrays in Java.

Slides:



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

Chapter 8: 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.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to arrays Data in economy size packages.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and 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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
DAT602 Database Application Development Lecture 5 JAVA Review.
1 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays 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.
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.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
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.
1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
Arrays. Arrays are objects that help us organize large amounts of information.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Introduction to programming in java Lecture 21 Arrays – Part 1.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Array in C# Array in C# RIHS Arshad Khan
Two-Dimensional Arrays
Array, Strings and Vectors
Lecture 5: Some more Java!
Java Arrays. Array Object An array :a container object holds a fixed number of values of a single type. The length of an array is established when the.
Java Language Basics.
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.
Data Structures and Database Applications Arrays And Lists
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
Arrays .
Chapter 7 The Java Array Object © Rick Mercer.
Single-Dimensional Arrays chapter6
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Question 1a) What is printed by the following Java program? int s;
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
C++ Array 1.
How do you do the following?
Arrays.
Presentation transcript:

Arrays in Java

Arrays in Java An array is collection of elements of similar type Array elements are always stored in consecutive memory blocks Arrays could be of primitive data types or reference type Arrays in Java are also objects An array holding 5 int elements Object of Rabbit Class on heap Array of References of Rabbit 22 33 66 100 72 www.prolearninghub.com

Arrays in Java Reference variables are used in Java to store the references of objects created by the operator - new Any one of the following syntax can be used to create a reference to an int array The reference x can be used for referring to any int array int x[ ]; int [ ] x; x null // Declart a reference to an int array int [ ] x; // Create a new int array and make x refer to it x = new int [5]; www.prolearninghub.com

Arrays in Java The following statement also creates a new int array and assigns its reference to x In simple terms, references can be seen as names of an array int [ ] x = new int [5]; X 1 Array Object 2 3 4 Subscript or Index www.prolearninghub.com

Arrays in Java An array is a groups like typed variables that is referred to a by a common type name. A specific element in an array is accessed by its index. Array offers a convenient meaning of grouping same information. www.prolearninghub.com

Arrays in Java Each value has a numeric index The entire array scores The entire array has a single name 0 1 2 3 4 5 6 7 8 9 79 87 94 82 67 98 87 81 74 91 An array of size N is indexed from zero to N-1 This array holds 10 values that are indexed from 0 to 9

Arrays in Java A particular value in an array is referenced using the array name followed by the index in brackets For example, the expression scores[2] refers to the value 94 (the 3rd value in the array) That expression represents a place to store a single integer and can be used wherever an integer variable can be used.

int[] scores = new int[10]; Arrays in Java Declaring Arrays The scores array could be declared as follows: int[] scores = new int[10]; The type of the variable scores is int[] (an array of integers) Note that the array type does not specify its size, but each object of that type has a specific size The reference variable scores is set to a new array object that can hold 10 integers An array is an object, therefore all the values are initialized to default ones (here 0) www.prolearninghub.com

Arrays in Java Array Example An array element can be assigned a value, printed, or used in a calculation: scores[2] = 89; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2; System.out.println ("Top = " + scores[5]); www.prolearninghub.com

Arrays in Java Array Example Another examples of array declarations : float[] prices = new float[500];boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750]; www.prolearninghub.com

Arrays in Java Initializing Arrays An array can be initialized while it is created as fallows: To refer any element of array we use subscript or index of that location First location of an array always has subscript 0 (Zero) For example: x [0] will give 1 c [1] will give b int [ ] x = {1, 2, 3, 4}; char [ ] c = { ‘a’, ‘b’, ‘c’}; www.prolearninghub.com

This works for any size array Arrays in Java Length of an Array Unlike C. Java checks the boundary of an array while accessing an element in it Programmer is not allowed to exceed its boundary And so, setting a for loop as follows is very common: for (int i = 0; i < x.length; ++i) { x [i] = 5; } X 1 length 5 2 3 4 This works for any size array Use the length attribute of an array object to get the length of array www.prolearninghub.com

Arrays in Java Array Example public class ArrayDemo { public static void main(String[ ] args) { int x[ ] = new int [5]; // loop to assign the values to array for(int i = 0; i < x.length; ++i){ x[i] =1+2; } // loop to print the values of array System.out.printIn(x[i]); www.prolearninghub.com

Arrays in Java Multidimensional Arrays 1 2 3 X int [ ][ ] x; //x is a reference to an array of int arrays x = new int[3][4]; //Create 3 new int arrays, each having 4 elements //x[0] refers to the first int array, x[l] to the second and so on //x[0][0] is the first element of the first array //x.length will be 3 //x[0].length, x[1].length and x[2].length will be 4 X 1 length 4 2 3 www.prolearninghub.com