Arrays Declare the Array of 100 elements Notes

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Lecture 05 - Arrays. Introduction useful and powerful aggregate data structure Arrays allow us to store arbitrary sized sequences of primitive values.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Declaring Variables You must first declare a variable before you can use it! Declaring involves: – Establishing the variable’s spot in memory – Specifying.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
Java Unit 9: Arrays Declaring and Processing Arrays.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
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.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
1 Class Chapter Objectives Use a while loop to repeat a series of statements Get data from user through an input dialog box Add error checking.
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.
Arrays Chapter 12. Overview Arrays and their properties Creating arrays Accessing array elements Modifying array elements Loops and arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter VII: Arrays.
Arrays.
Arrays Chapter 7.
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.
Two-Dimensional Arrays
Two-Dimensional Arrays
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Counted Loops.
Siti Nurbaya Ismail Senior Lecturer
CSC 142 Computer Science II
Arrays, Part 1 of 2 Topics Definition of a Data Structure
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CSS161: Fundamentals of Computing
Arrays An Array is an ordered collection of variables
Yong Choi School of Business CSU, Bakersfield
Nested Loop Review and Two-Dimensional Arrays
7 Arrays.
CNG 140 C Programming (Lecture set 8)
Building Java Programs
Review of Arrays and Pointers
Introduction To Programming Information Technology , 1’st Semester
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Starting Out with Programming Logic & Design
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Two-Dimensional Arrays
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays Week 2.
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Building Java Programs
Arrays I Handling lists of data.
Arrays in Java.
Building Java Programs
Building Java Programs
Question 1a) What is printed by the following Java program? int s;
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Arrays, Part 1 of 2 Topics Definition of a Data Structure
Arrays, Part 1 of 2 Topics Definition of a Data Structure
How do you do the following?
Week 7 - Monday CS 121.
Presentation transcript:

Arrays Declare the Array of 100 elements Notes Definition: An indexed table of variables in memory Declare the Array of 100 elements Integers: int[] integers = new int[100]; Strings: String[] strings = new String[100]; Doubles: double[] doubles = new double[100]; Declare and initialize at the same time (initializer list) Six integers: int[] integers = {3, 4, 5, 6, 7, 8}; Four Strings: String[] strings = {“abc”, “def”, “ghi”, “jkl”}; Five doubles: Doubles[] doubles = {3.3, 4.4, 5.5, 6.6, 7.7}; Notes Without initializer lists, it would take seven statements to initialize the array (see next slide) String[] strings = new String[100] puts a null in each spot in the table. null is a reserved word meaning nothing is there yet.

Array Example int nums[] = new int[6]; nums[0] = 10; nums[1] = 20; nums[2] = 22; nums[3] = 33; nums[4] = 50; nums[5] = 66; int nums[] = {10,20,22,33,50,66}; What prints? System.out.println(nums[4]); Replace an element nums[3] = 99; Note: Java arrays first element always is index zero. Find an array’s length System.out.println(nums.length) Index Content 1 2 3 4 5 10 20 22 nums 33 50 66

Two column (dimensioned) array Declare int[][] twoCols = { {11,22,33,44,55}, {1,99,88,77,66}, {2,3,4,5,6}, {23,12,9,8,7}, {34,45,56,67,89}, {65,54,43,32,21}, {76,87,13,24,35} }; Note: initializer lists are a another use of {} to enclose initial array element values Two column (dimensioned) array twoCols 1 2 3 4 11 22 33 44 55 99 88 77 66 5 6 23 12 9 8 7 34 45 56 67 89 65 54 43 32 21 76 87 13 24 35 Access and print row 3 column 4: System.out.println(twoCols[3][4]); prints a 7 Replace the 76 in row 6, column 0 with a 63: twoCols[6][0] = 63;

Why Arrays? Answer: With counter-controlled loops, we can do lots with few statements! Fill a one column array with a value for (int i=0; i<nums.length; i++) nums[i] = 0; Sum up the elements in an array int sums = 0; for (int i=0; i<nums.length; i++) { sum += nums[i]; } System.out.println(sum); Find the biggest number in the array int max = nums[0]; for (int i=0; i<nums.length; i++) if (max < nums[i]) max = nums[i]; System.out.println(“Maximum is “ + max); Question: Why doesn’t the if statement in the for loop need braces around it?

Example: Member of a club String person = IO.readString(“What’s your name”); for (int i=0; i<names.length; i++) { found = false; if (person.equals(names[i])) { found = true; break; } if (found) System.out.prinltn(“OK to enter”); else System.out.println(“Lock them up”); We have an array of Strings Each string is the name of a person in the Computer Science club We need to know if someone should be allowed admission How do we determine if that person is in the member list? Question: Why compare with person.equals(names[i]) and not == names[i]?

Example: Is it a magic square? boolean magicS = true; int sum, magicTotal = 0; for (int row = 0; row<magic.length; row++) { sum = 0; for (int c=0; c<magic[c].length; c++) sum += magic[row][c]; if (row==0) magicTotal = sum; else if (magicTotal!=sum) { magicS= false; break; } } if (magicS) System.out.println(“Rows OK”); else System.out.println(“Rows No Good”); A magic square is one where all the rows, columns, and diagonals sum to the same total. We want to see if a two-column array magic is a magic square. The example on the right tests the rows How would we test the columns? How would we test the diagonals?

Putting it all together Enter a series of grades and compute the high, low and average score public class Test { public static void main(String[] args) { int count = 0; double total = 0, avg, min, max, scores[] = new double[100], score, low, high; while ((count<100) && ( (score=IO.readDouble("Enter score, -1 to quit")) > 0) ) { scores[count++] = score; } // What happens if we allowed if (count==0) System.exit(0); min = max = scores[0]; for (int i=0; i<count; i++) { total += scores[i]; if (scores[i] < min) min = scores[i]; if (scores[i] > max) max = scores[i]; } avg = total/count; System.out.printf("min=%.2f max=%.2f avg=%.2f",min, max, avg); } } Question: Why do we need to make sure that count is greater than zero?

Review What is an array? Why are arrays useful? What is an initializer list? Why are they useful? What is the index to an array? What is the highest index to an array of ten elements? What is an array’s dimension? What is the purpose of the new reserved word? What is an array element? Explain the syntax of the for loop construct. How do you determine the size of a single dimensioned array? How about the second dimension of a two dimensioned array? How do you store into an array? How do you retrieve data from an array?