Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Arrays.
1 Various Methods of Populating Arrays Randomly generated integers.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
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.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
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.
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.
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
Computer Science 210 Computer Organization Arrays.
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.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
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.
Chapter 4: Control Structures II
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.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
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.
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.
Arrays.
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.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
ARRAY AND LOOPS REVIEW Mr. Crone. What will the code below print? int[] arry = {2, 5, 2, 1, 3}; for(int i = 0; i < 5; i ++) System.out.print(arry[i]);
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Chapter VII: Arrays.
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.
TK1114 Computer Programming
Arrays Declare the Array of 100 elements Notes
CSS161: Fundamentals of Computing
Arrays An Array is an ordered collection of variables
Nested Loop Review and Two-Dimensional Arrays
Review of Arrays and Pointers
Data Structures (CS212D) Week # 2: Arrays.
Presentation transcript:

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

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 nums

Two column (dimensioned) array Access and print row 3 column 4: System.out.println(twoCols[3][4]); prints a 7 2.Replace the 76 in row 6, column 0 with a 63: twoCols[6][0] = 63; 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: initialization lists are a second use of {} in Java twoCols

Why Arrays? 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); Answer: With counter-controlled loops, we can do lots with few statements! Question: Why doesn’t the if statement in the for loop need braces around it?

Example: Member of a club 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? 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”); Question: Why compare with person.equals(names[i]) and not == names[i]?

Example: Is it a magic square? 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? 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”);

Putting it all together public static void main(String[] args) {int count = 0, low = 0, high = 0, score; double total = 0, avg; int[] scores = new int[100]; while ( (count 0) ) { scores[count++] = score; } // What happens if we allowed count = 100? if (count>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.println(“min = ” + min + “ max = ” + max + “avg = ” + avg); } Enter a series of grades and compute the high, low and average score Question: Why do we need to make sure that count is greater than zero?

Review 1.What is an array? 2.Why are arrays useful? 3.What is an initializion list? Why are they useful? 4.What is the index to an array? 5.What is the highest index to an array of ten elements? 6.What is an array’s dimension? 7.What is the purpose of the new reserved word? 8.What is an array element? 9.Explain the syntax of the for loop construct. 10.How do you determine the size of a single dimensioned array? How about the second dimension of a two dimensioned array? 11.How do you store into an array? 12.How do you retrieve data from an array?