Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.

Slides:



Advertisements
Similar presentations
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Advertisements

Chapter 10 Introduction to Arrays
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Arrays Chapter 6 Chapter 6.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
1 2-D Arrays Overview l Why do we need Multi-dimensional array l 2-D array declaration l Accessing elements of a 2-D array l Declaration using Initializer.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
©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 Lecture Today’s topic Arrays Reading for this Lecture: –Chaper 11.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Multiple-Subscripted Array
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
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.
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.
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 – Part II Lecture Notes 4. Arrays An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 7 Multidimensional.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
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.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
int [] scores = new int [10];
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
ARRAYS Multidimensional realities Image courtesy of
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. System.out.println(“Please enter grade.
Computer Programming BCT 1113
Chapter 8: Collections: Arrays
1-Dimensional Arrays 2-Dimensional Arrays => Read section 1.4
int [] scores = new int [10];
int [] scores = new int [10];
Suggested self-checks: Section 7.11 #1-11
Arrays Introduction to Arrays Reading for this Lecture:
Presentation transcript:

Lecture 05 - Arrays

Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values or sequences of references to objects easily access and manipulate the values/objects that they store Arrays are indexed by a sequence of integers classes use arrays as instance variables to store databases of value/references

Declaring Arrays int[] A = new int[5];

More About Arrays an array is a homogeneous data structure: each of its members stores the same type (either primitive or reference) the indexes go from 0 to one less than the length of the array each array object stores a public final int length instance variable that stores the length of the array we can access the value stored in this field, in the example above, by writing a.length int[] a = new int[]{4, 2, 0, 1, 3}; another way to create an array

Arrays

Accessing Arrays int[] a = new int[]{4, 2, 0, 1, 3}; System.out.println( a[0] ); if (a[2] == 0)...some statement if the value computed for the index is less than 0, or greater than OR EQUAL TO the length of the array – trying to access the memember at that index cause Java to throw the ArrayIndexOutOfBoundsException which contains a message showing what index was attempted to be accessed An array of Strings – String[] s = new String[]{"ABC", "LMN", "XYZ"};

A Graphical View

Processing Arrays Typically, a for loop is used to generate all the indexes for the array object. Study the following code carefully; int sum = 0; for (int i=0; i < a.length; i++) – sum += a[i]; System.out.println("Sum = " + sum);

More Code What is the output of the following code? for (int i=0; i < a.length; i++) System.out.print(a[i]+" "); System.out.println(); The following code computes and prints the maximum value stored in an array int max = a[0]; for (int i=1; I<a.length; i++) if (a[I] > max) max = a[i]; System.out.println("Max = " + max);

Arrays - Review An array is a data structure that groups and organizes data l Array is a list of values (int, double, aggregates) l The number corresponding to each position is called an index or subscript Index = 0 Value = 12 Index = 2 Value = 16 Index = 1 Value = 10

Declaring and using Arrays Arrays are objects Int [] height = new int[11] New operator allocates memory space to save values The type of the array is int [] Eg: Random x = new Random(); final int LIMIT = 15; int [] list = new int[LIMIT] for (int I=0; I<LIMIT;I++) { list[I] = x.nextInt(LIMIT);} for (int I=0; I<LIMIT;I++) { System.out.print(list[I]+”\t”);}

Declaring and using Arrays Two ways to declare arrays: int [] grades; int grades[]; No difference as far as compiler is concerned First one is more consistent with type declarations int [] A, B, C; int A[], B, C[];

Interactively Read a set of numbers into an Array import javax.swing.JOptionPane; boolean done = false; final int LIMIT = 15; int [] list = new int[LIMIT]; int index = 0; Int num; While (true) { num =Integer.parseInt( JOptionPane.showInputDialog("Enter an integer")); if (I==LIMIT-1 || num==-999) break; else list[I++] = num; }

An Array Example final int NUMCHARS = 26; String line = JOptionPane.showInputDialog("Enter a sentence"); char current; int other= 0; int [] upper = new int[NUMCHARS]; For (int I=0; I<line.length(); I++) { current = line.charAt(I); if (current >= ‘A’ && current <= ‘Z’) upper[current-’A’]++; } What does above program do?

More on Arrays Array Intializer int [] list = {1,2,3,4}; Array as Parameter An entire array can be passed as a parameter to a method A copy of the reference to the array is passed A method can change array elements permanently A method cannot change the reference itself

Two Dimensional Arrays (Matrices) A Table (Grid) of Rows and Columns Uses Two Indexes to refer to an element Two Dimensional Array(or Matrix) is an array of Arrays Eg: int [][] Table = new int[5][10]; for (int I=0; I<Rows; I++) for (int j=0;j<Cols;j++) Table[I][j]=I+j; for (int I=0;I<Table.length; I++) for (int j=0;j<Table[I].length;j++) System.out.print(Table[I][j]);

Two Dimensional Arrays ctd.. Int [][] Scores = {{2,3,3},{2,2,2}}; Defines a 2 by 3 matrix (I.e 2 rows and 3 columns) Finding Row Sum of Row 0 (first row) for (int j=0; j<Scores[0].length;j++) sum += Scores[0][j]; Finding Column Sum of Column 2 (third column) for (int i=0; i<Scores.length;i++) sum += Scores[i][2];

Exercises Write a method to find the row sum of a matrix Write a method to find the column sum of a matrix Write a method to find the maximum element in a matrix Write a method to swap two rows of a matrix