Problem Solving using the Java Programming Language 17-21 May 2010 Mok Heng Ngee Day 5: Arrays.

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

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
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.
CSC 142 K 1 CSC 142 Multidimensional Arrays [Reading: chapter 10]
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Arrays. Memory organization Table at right shows 16 bytes, each consisting of 8 bits Each byte has an address, shown in the column to the left
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.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
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.
Java Syntax Primitive data types Operators Control statements.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Arrays Declare the Array of 100 elements 1.Integers: int[] integers = new int[100]; 2.Strings: String[] strings = new String[100]; 3.Doubles: double[]
Multi-Dimensional Arrays Rectangular & Jagged Plus: More 1D traversal.
Java Unit 9: Arrays Declaring and Processing Arrays.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 10 Thinking in Objects.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
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.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
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.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
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.
ARRAYS Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
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.
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.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Types in Java 8 Primitive Types –byte, short, int, long –float, double –boolean –Char Also some Object types: e.g. “String” But only single items. What.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CSC1030 HANDS-ON INTRODUCTION TO JAVA Introductory Lab.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
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.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
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.
int [] scores = new int [10];
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
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.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Java Programming Language Lecture27- An Introduction.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
Introduction to Computing Using Java
Building Java Programs
CSC 142 Computer Science II
Building Java Programs Chapter 7
An Introduction to Java – Part I, language basics
Building Java Programs
Building Java Programs
Building Java Programs
CS2011 Introduction to Programming I Arrays (II)
python.reset() Also moving to a more reasonable room (CSE 403)
int [] scores = new int [10];
CSS161: Fundamentals of Computing
Building Java Programs
Building Java Programs
Arrays Wellesley College CS230 Lecture 02 Thursday, February 1
COS 151 Bootcamp – Week 4 Department of Computer Science
Presentation transcript:

Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays

Arrays We will only deal with arrays of primitive types in this course: – E.g. an array of ints or an array of booleans What is an array? – A collection of elements. – An array of ints is a collection of ints

Arrays (2) How to declare an array variable: int[] a; How to create the array itself using the new keyword: a = new int[3]; // creates an array for 3 ints This creates an array of 5 booleans: boolean[] b = new boolean[5];

Arrays (3) By default, all elements in the array will be 0, false or null (depending on the type) You can set the values in the array: a[0] = 30; a[1] = 40; a[2] = 50; Notice that the array index starts from 0! How to get values from the array: int x = a[2]; System.out.println(x); // will print out 50

Arrays (4) What is an ArrayIndexOutOfBoundsException? – When you try to use an index which is out of range – E.g.: setting the 4 th element of the array when there are only 3 elements: a[3] = 60; // index 3 refers to the 4 th element or retrieving an invalid element: int i = a[-2];

Arrays (5) Use.length to find out how many elements there are in an array: int []a = new int[15]; System.out.println ( a.length ); Can methods take in arrays and return arrays? public static int[] doSomething ( double[] x ){ … }

Try it Write a class which contains a main method which: – Declares & creates an array of 5 boolean values – Sets the values to true, false, true, false, true – Invokes the printArray method Write another method called printArray which: – Takes in an array of boolean values – Prints out the values in one row – Returns nothing

Arrays (6) “Lazy” way to initialize an array - the following 2 sections of code are identical: int []a = new int[3]; a[0] = 9; a[1] = 8; a[2] = 7; int []a= {9,8,7};

2D Arrays A 1D array can be viewed as a single row A 2D array is an array of arrays: – Can be viewed as a table of rows and columns Declare & create a 2D array of ints with 3 rows & 2 columns. int [][] a = new int[3][2];

2D Arrays (2) Setting values in a 2D array: a[0][0] = 3; a[0][1] = 9; Getting values from a 2D array: int x = a[2][1] ; Very flexible storage mechanism to represent a table of values

2D Arrays (3) a.length gives the number of rows a[0].length gives the number of columns in the 1 st row a[1].length gives the number of columns in the 2 nd row & so on

Example: getting the sum of all ints in a 2D array public class Test{ public static void main (String[] args){ int[][] a = new int [3][4]; // code to initialize values in a... int sum = 0; for (int i=0; i<a.length; i++){ for (int j=0; j<a[i].length; j++){ sum += a[i][j]; } System.out.println(sum); }

2D Arrays (4) “Lazy” way to initialize a 2D array: int [][]a = {{9,8},{1,7},{-1,2}};

Summary Day1 : Introduction to the JDK, variables, operators Day 2 : looping Day 3 : decisions Day 4 : methods & 1D arrays Day 5: 2D arrays

Objectives achieved? To exercise your systematic thinking skills in logical problem solving To introduce you to Java To let you experience SMU’s learning culture Have you had fun?