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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
Chapter 5 Arrays and Vectors An array allows you to group data items together in a structure that is processed via an index. They allow you to process.
Arrays Chapter 6 Chapter 6.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Java Syntax Primitive data types Operators Control statements.
©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.
Arrays CS Feb Announcements Exam 1 Grades on Blackboard Project 2 scores: end of Class Project 4, due date:20 th Feb –Snakes & Ladders Game.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter7: Arrays.
Sanjay Goel, School of Business, University at Albany, SUNY 1 MSI 692: Special Topics in Information Technology Lecture 4: Strings & Arrays Sanjay Goel.
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.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1 Lecture # 4. * An array is a group of contiguous or related data items that share a common name. * Each value is stored at a specific position * Position.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA Arrays. Objectives Be able to declare and initialize arrays Be able to conceptualize (draw) how arrays are represented in computer memory. Be able.
Arrays. Arrays in Java  Arrays in Java are objects.  Like all objects are created with the new keyword.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
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.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
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.
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.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
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
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.
SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
CIS162AD - C# Arrays – part 1 12_arrays_loading.ppt.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Peyman Dodangeh Sharif University of Technology Fall 2013.
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.
Question 1a) What is printed by the following Java program? int s; int r; int i; int [] x = {4, 8, 2, -9, 6}; s = 1; r = 0; i = x.length - 1; while (i.
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.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 7.
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.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
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.
Array and String.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
Asserting Java © Rick Mercer Chapter 7 The Java Array Object.
© Rick Mercer Chapter 7 The Java Array Object.  Some variables store precisely one value: a double stores one floating-point number a double stores one.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Object Oriented Programming Lecture 2: BallWorld.
Chapter 6: Using Arrays.
An Introduction to Java – Part I, language basics
Object Oriented Programming in java
Chapter 7 The Java Array Object © Rick Mercer.
Arrays in Java.
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
Presentation transcript:

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 basic way to store large numbers of values.

Array declaration Array declaration format [ ] Examples: int scores[]; double[] args;

Array Creation [ ] = new [size] int[] anArray; anArray = new int[10]; /*allocates memory for 10 integers */ float anotherArray[]; anotherArray = new float[10]; // array of 10 floats

Java Array Default Values After you instantiate an array, default values are automatically assigned to it in the following manner. byte – default value is zero short – default value is zero int – default value is zero long – default value is zero, 0L. float – default value is zero, 0.0f. double – default value is zero, 0.0d. boolean – default value is false. reference types – default value is null.

Accessing arrays Each value is accessed by specifying an integer subscript or index in brackets following the array name. float marks[] = new float[20]; marks[0], marks[1] … marks[19] The index for an array must be between 0 and (length of the array -1) The length of the array can be found using the property of the array, array_name.length

Arrays: Continued... It is possible to declare and initialize an array at the same time. int x[] = {0, 1, 2} x.length = 3 You can also initialize array values using a loop. int x[] = new int[3]; for(int i = 0; i < x.length; i++) { x[i] = i; }

public class Main { public static void main(String[] args) throws Exception { int[] array1 = {1,2,3,4}; int[] array2 = {1,2,3,4}; int[] array3 = {1,2,3}; System.out.println("Comparing array1 and array 2" + Arrays.equals(array1, array2)); System.out.println("Comparing array1 and array 3“ + Arrays.equals(array1, array3)); }

Result Is array 1 equal to array 2?? true Is array 1 equal to array 3?? false The Arrays class contains various methods for manipulating arrays (such as sorting and searching).

Array of Objects Similar to array of primitive types. Student students[] = new Student[3]; This creates an array of references to hold 3 Student objects. students[0] = new Student(); students[1] = new Student(); students[2] = new Student(); for(int i = 0; i < students.length; i++) { students[0] = new Student(); }

Searching through the array int cnt = 0; for(int i = 0; i < a.length; i++) { if(a[i] == 100) { cnt++; } for(int i = 0; i < students.length; i++) { if(students[i].name.equals(“Tom”)) { System.out.println(“Tom ID: “ + students[i].id); }

Deleting an array entry Deleting a Student Tom Object. for(int i = 0; i < students.length; i++) { if(students[i].name.equals(“Tom”)) { students[i] = null; } Deleting a array entry doesn't actually shrink the array.

public class Example { public static void main(String[] args) { int x[] = {0, 1, 2, 3, 4, 5}; int median = 0; if(x.length %2 == 0) { median = (x[x.length/2] + x[(x.length/2)+1])/2; } else { median = x[x.length/2]; }

Using arrays in methods public void changeValue(float[] numbers) { } float x[] = new float[5]; changeValue(x); //Method Call During the method call, the value of the argument (which is an address) is copied to the method parameter (numbers). Both numbers and x now point to the same address.

Two Dimensional Arrays 2-dimensional arrays are usually represented with a row-column "spreadsheet" style. Assume we have an array, a, with two rows and four columns. int[][] a = new int[2][4]; a[0][0]a[0][1]a[0][2]a[0][3] a[1][0]a[1][1]a[1][2]a[1][3]

Declaration [][] Creation =new [ ][ ] int x[][]; x = new int[10][12]; // table of 10 rows and 12 columns

Searching a two dimensional array for (int i = 0; i < scoreTable.length; i++) { for (int j = 0; j < scoreTable[i].length; j++) { if(scoreTable[i][j] == 100) { System.out.println(“Score is 100”); break; }

public class Main { public static void main(String[] args) throws Exception { int A[][] = new int[3][4]; int sum = 0; for (int i = 0; i < 3; i++) for (int j = 0; j < 4; j++) sum = sum + A[i][j]; System.out.println(“Sum is “ + sum); }

Jagged Arrays 2D arrays need not be rectangular. You can create a 2D array with m rows and each row have different number of slots. subarrays may have different lengths. double scoreTable = new double[4][ ]; for (int i = 0; i < 4; i++) scoreTable[i] = new double [i + 1];