INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter7: Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
1 Arrays In many cases we need a group of nearly identical variables. Example: make one variable for the grade of each student in the class This results.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 Arrays & functions Each element of an array acts just like an ordinary variable: Like any ordinary variable, you can pass a single array element to a.
 Pearson Education, Inc. All rights reserved Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Java Unit 9: Arrays Declaring and Processing Arrays.
03/16/ What is an Array?... An array is an object that stores list of items. Each slot of an array holds an individual element. Characteristics.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
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.
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.
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.
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 Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Java SE 8 for Programmers, Third Edition
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.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
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.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
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.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Aside: Running Supplied *.java Programs Just double clicking on a *.java file may not be too useful! 1.In Eclipse, create a project for this program or.
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 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
 Pearson Education, Inc. All rights reserved Passing Arrays to Methods To pass array argument to a method – Specify array name without.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
A DVANCED P ROGRAMMING C HAPTER 8: A RRAYS Dr Shahriar Bijani Spring 2016.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
Arrays Chapter 7.
Advanced Programming Chapter 8: Arrays
Java How to Program, Late Objects Version, 10/e
An Introduction to Java – Part I, language basics
Object Oriented Programming in java
CS2011 Introduction to Programming I Arrays (II)
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
Arrays in Java.
Presentation transcript:

INSTRUCTOR: SHIH-SHINH HUANG Windows Programming Using Java Chapter7: Arrays

Contents Introduction Declaring and Creating Arrays Array Passing Multidimensional Arrays Variable-Length Argument

Introduction Primitive Type  A primitive type variable can store exactly one value of its declared type at a time.  Primitive-type instance variables are initialized by default.  Local variable are not initialized by default.  The primitive types in java are boolean, byte, char, short, int, long, float, and double.

Introduction Primitive Type int count=0; count=[0004h] [0004h]=0 count=100; [0004h]=100;

Introduction Reference Type  Program use variables of reference types to store locations of objects in the memory.  Such a variable is said to refer to an object.  Reference-type instance variable are initialized by default to the value null.  The general reference types in Java are objects and array.

Introduction Reference Type GradeBook javaGradeBook; javaGradeBook=[0004h] [0004h]=0 javaGradeBook = new GradeBook(); [0004h]=0010h attributes initialization

Introduction Reference Type javaGradeBook.CourseName=“java” mem = [0004h]; /* mem=0010h */ mem + offset CourseName =“java”

Introduction Array Description  Array are data structures consisting of related data items of the same type.  Arrays are the reference types that refer to array instances in memory.  The elements of an array can be either value or reference types. 8

Introduction Array-Access Expression  It provides the way to access the array elements.  Expression Constituent  Name of Array  Index in Square Brackets []  The index starts from zero and must be a nonnegative integer 9

Introduction c[ 11 ] c[ 10 ] c[ 9 ] c[ 8] c[ 7 ] c[ 4 ] c[ 3 ] c[ 2 ] c[ 1 ] c[ 0 ] c[ 6 ] c[ 5 ] Position Number (index or subscript) of the element within array c Name of array ( c )

Declaring and Creating Arrays Array Creation Expression  Arrays are created with keyword new  The program specifies  the type of array  the number of array.  When an array is created, each element recevies a default value. int c[]; c = new int[12]; int c[] = new int[12]; int[] c; c = new int[12];

Declaring and Creating Arrays Array Initializier  An array can be created and initialized its elements at the same time.  An array initializer is a commoa-separated list of expressions enclosed in braces. Array Length  Every array instance has its own length which can be accessed with the length property. int c[] = {10, 20, 30, 40, 50}; c.length

Declaring and Creating Arrays public class ScoreBook { int m_iScoreArray[] = {50, 99, 40, 20, 30, 35, 90, 91, 89}; public int Average(){ int sum = 0; for(int i=0; i < m_iScoreArray.length; i++) sum = sum + m_iScoreArray[i]; return sum / m_iScoreArray.length; }/* End of Average */ public void DrawBar(){ …… }/* End of DrawBar */ }/* End of ScoreBook */

Declaring and Creating Arrays public class ScoreBook { public int Average(){}/* End of Average */ public void DrawBar(){ int countArray[] = new int[10]; for(int i=0; i < m_iScoreArray.length; i++){ int index = m_iScoreArray[i] / 10; countArray[index]++; }/* End of for-loop */ System.out.println("Grade Distribution"); for(int i=0; i < 10 ; i++){ System.out.printf("%2d-%2d: ", i*10, i*10+9); for(int j=0; j < countArray[i]; j++) System.out.print("*"); System.out.println(); }/* End of for-loop */ }/* End of DrawBar */ }/* End of ScoreBook */

Declaring and Creating Arrays public class ScoreBookTest { public static void main(String args[]){ ScoreBook scoreBook = new ScoreBook(); int average = scoreBook.Average(); System.out.printf("Average Score: %d \n", average); scoreBook.DrawBar(); }/* End of main */ }/* End of ScoreBookTest */ Average Score: 60 Grade Distribution 0- 9: 10-19: 20-29: * 30-39: ** 40-49: * 50-59: * 60-69: 70-79: 80-89: * 90-99: ***

Array Passing Argument Passing  Pass-by-Value  A copy of a the argument is passed to the called method  Changes to the called method’s copy do not affect the original one. int i =1; Modify(i);

Array Passing Argument Passing  Pass-by-Reference  The called method can access the argument’s value directly and modify the value.  It improves performance by eliminating the need to copy the data. int[] array={10,20}; Modify(array);

Array Passing Passing Array Elements  We use the indexed name as an argument.  This is a kind of pass-by-value, that is, the change to the value can not affect the array element. void Modify(int element){ …… }/* End of Modify */ int[] array={10, 20, 100}; Modify(array[1]);

Array Passing Passing Array  We specify the array name without any brackets.  The called method receives the copy of the reference. void Modify(int[] ary){ …… }/* End of Modify */ int[] array={10, 20, 100}; Modify(array);

Array Passing public class PassArray { public void ModifyElement(int element){ element = element * 2; System.out.printf("Value in ModifyElement: %d \n", element); }/* End of ModifyArray */ public void ModifyArray(int array[]){ for(int i=0; i < array.length; i++) array[i] = array[i] * 2; }/* End of ModifyArray */ }/* End of PassArray */

Array Passing public class PassArrayTest { public static void main(String args[]){ PassArray passArray = new PassArray(); int array1[]={1, 2, 3, 4, 5}; System.out.printf("Befor ModifyElement :%d \n", array1[1]); passArray.ModifyElement(array1[1]); System.out.printf("Afer ModifyElement :%d \n", array1[1]); int array2[]={1, 2, 3, 4, 5}; System.out.printf("Befor ModifyArray \n"); for(int i=0; i < array2.length; i++) System.out.printf("%d ", array2[i]); passArray.ModifyArray(array2); System.out.printf("\nAfter ModifyArray \n"); for(int i=0; i < array2.length; i++) System.out.printf("%d ", array2[i]); }/* End of main */ }/* End of PassArrayTest */ Befor ModifyElement :2 Value in ModifyElement: 4 Afer ModifyElement :2 Befor ModifyArray After ModifyArray

Multidimensional Arrays Description  It is used to represent tables of values arranged in rows and columns.  There are two types of 2D array Rectangular ArrayJagged Array

Multidimensional Arrays Rectangular Array  An array with m rows and n columns is called an m-by-n array.  Array-Creation Expression  It can be created and initializes its elements. int c[][]; c = new int[3][4]; int c[][] = new int[3][4]; int b[][]={{1,2}, {3, 4}};

Multidimensional Arrays Jagged Array  A jagged array is a 1D array where each element refers to a 1D array.  The lengths of the rows can be different.  Array-Creation Expression int c[][]; c = new int[2][]; c[0] = new int[5]; c[1] = new int[3]; int c[][]= {{1,2}, {3}, {4, 5, 6}};

Multidimensional Arrays public class InitArrayTest { public static void main(String args[]){ InitArray initArray = new InitArray(); int array1[][] = {{1,2,3}, {4, 5, 6}}; System.out.println("Values in Array1"); initArray.OutputArray(array1); int array2[][] = {{1,2}, {3}, {4, 5, 6}}; System.out.println("Values in Array2"); initArray.OutputArray(array2); }/* End of main */ }/* End of InitArrayTest */

Multidimensional Arrays Values in Array Values in Array public class InitArray { public void OutputArray(int array[][]){ for(int i=0; i < array.length; i++){ for(int j=0; j < array[i].length; j++) System.out.printf("%d ", array[i][j]); System.out.println(); }/* End of for-loop*/ }/* End of OutputArray */ }/* End of InitArray */

Variable-Length Argument Description  It allows a method to receive arbitrary number of arguments.  An argument type is followed by an ellipsis (…) in a method’s parameter list.  The use of ellipsis can occur only in the last entry of the parameter list. double Average(double … numbers);

Variable-Length Argument public class VarArgs { public double Average(double... numbers){ double sum = 0.0; for(int i=0; i < numbers.length; i++) sum += numbers[i]; return sum / numbers.length; }/* End of Average */ }/* End of VarArgs */ The variable-length argument is considered as an array

Variable-Length Argument public class VarArgsTest { public static void main(String args[]){ VarArgs varArgs = new VarArgs(); double d1= 10.0, d2= 20.0, d3= 30.0; System.out.printf("Average of d1 and d2: %f \n", varArgs.Average(d1,d2)); System.out.printf("Average of d1, d2, and d3: %f \n", varArgs.Average(d1, d2, d3)); }/* End of main */ }/* End of VarArgsTest */ Average of d1 and d2: Average of d1, d2, and d3: