Lecture 07. Do not have to create an array while declaring array variable [] variable_name; int [] prime; int prime[]; Both syntaxes are equivalent No.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
Arrays in JAVA CS 340 Software Design Muhammad Talha.
Warm up Determine the value stored in each of the following: – gradeTable[ 0 ][ 0 ] – gradeTable[ 1 ][ 1 ] – gradeTable[ 3 ][ 4 ] – gradeTable[ 5 ][ 2.
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.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Strings and Arrays The objectives of this chapter are:  To discuss the String class and some of its methods  To discuss the creation and use of Arrays.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
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.
Arrays. Arrays  When a value is to be used in a program, a variable is declared to hold that value  What about storing the results of exams for a large.
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
From C++ to Java A whirlwind tour of Java for C++ programmers.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
Introduction to Java Java Translation Program Structure
August 6, 2009 Data Types, Variables, and Arrays.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
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.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
2D Arrays A multi dimensional array is one that can hold all the values above. You set them up like this: int[ ][ ] gradeTable = new int[7][5]; With the.
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.
Review Pointer Pointer Variables Dynamic Memory Allocation Functions.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
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.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Multidimensional Arrays tMyn1 Multidimensional Arrays It is possible to declare arrays that require two or more separate index values to access an element.
 Introducing Arrays  Declaring Array Variables, Creating Arrays, and Initializing Arrays  Copying Arrays  Multidimensional Arrays  Search and Sorting.
Exception Handling How to handle the runtime errors.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lecture #15 ARRAYS By Shahid Naseem (Lecturer). 2 ARRAYS DEFINITION An array is a sequence of objects of same data type. The objects in an array are also.
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.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
Java Programming Language Lecture27- An Introduction.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Array in C# Array in C# RIHS Arshad Khan
Arrays in JAVA Visit for more Learning Resources.
Elementary Programming
Multiple variables can be created in one declaration
Fifth Lecture Arrays and Lists
Object Oriented Programming
IDENTIFIERS CSC 111.
OPERATORS (2) CSC 111.
An Introduction to Java – Part I, language basics
Chapter 2: Java Fundamentals
Arrays in Java.
Winter 2019 CMPE212 4/7/2019 CMPE212 – Reminders
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Lecture 07

Do not have to create an array while declaring array variable [] variable_name; int [] prime; int prime[]; Both syntaxes are equivalent No memory allocation at this point

Define an array as follows: variable_name=new [N]; primes=new int[10]; Declaring and defining in the same statement: int[] primes=new int[10]; In JAVA, int is of 4 bytes, total space=4*10=40 bytes

prime Index value

We define int[] prime=new long[20]; MorePrimes.java:5: incompatible types found: long[] required: int[] int[] primes = new long[20]; ^ The right hand side defines an array, and thus the array variable should refer to the same type of array

We define int prime[100]; MorePrimes.java:5: ']' expected The C++ style is not permitted in JAVA syntax

Valid code: int k=7; long[] primes = new long[k]; Invalid Code: int k; long[] primes =new long[k]; Compilation Output: MorePrimes.java:6: variable k might not have been initialized long[] primes = new long[k]; ^

When array is created, array elements are initialized Numeric values (int, double, etc.) to 0 Boolean values to false Char values to ‘\u0000’ (unicode for blank character) Class types to null

Index of an array is defined as Positive int, byte or short values Expression that results into these types Any other types used for index will give error long, double, etc. Incase Expression results in long, then type cast to int Indexing starts from 0 and ends at N-1 primes[2]=0; int k = primes[2]; …

JAVA checks whether the index values are valid at runtime If index is negative or greater than the size of the array then an IndexOutOfBoundException will be thrown Program will normally be terminated unless handled in the try {} catch {}

long[] primes = new long[20]; primes[25]=33; …. Runtime Error: Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 25 at MorePrimes.main(MorePrimes.java:6)

Array variable is separate from array itself Like a variable can refer to different values at different points in the program Use array variables to access different arrays int[] primes=new int[10]; …… primes=new int[50]; Previous array will be discarded Cannot alter the type of array

Initialize and specify size of array while declaring an array variable int[] primes={2,3,5,7,11,13,17}; //7 elements You can initialize array with an existing array int[] even={2,4,6,8,10}; int[] value=even; One array but two array variables! Both array variables refer to the same array Array can be accessed through either variable name

even value

long[] primes = new long[20]; primes[0] = 2; primes[1] = 3; long[] primes2=primes; System.out.println(primes2[0]); primes2[0]=5; System.out.println(primes[0]);

2525

Refer to array length using length A data member of array object array_variable_name.length for(int k=0; k<primes.length;k++) …. Sample Code: long[] primes = new long[20]; System.out.println(primes.length); Output: 20

If number of elements in the array are changed, JAVA will automatically change the length attribute!

class MinAlgorithm { public static void main ( String[] args ) { int[] array = { -20, 19, 1, 5, -1, 27, 19, 5 } ; int min=array[0]; // initialize the current minimum for ( int index=0; index < array.length; index++ ) if ( array[ index ] < min ) min = array[ index ] ; System.out.println("The minimum of this array is: " + min ); } 41

Two-Dimensional arrays float[][] temperature=new float[10][365]; 10 arrays each having 365 elements First index: specifies array (row) Second Index: specifies element in that array (column) In JAVA float is 4 bytes, total Size=4*10*365=14,600 bytes

Sample[0] Sample[1] Sample[2]

int[][] array2D = { {99, 42, 74, 83, 100}, {90, 91, 72, 88, 95}, {88, 61, 74, 89, 96}, {61, 89, 82, 98, 93}, {93, 73, 75, 78, 99}, {50, 65, 92, 87, 94}, {43, 98, 78, 56, 99} }; //5 arrays with 5 elements each

All arrays do not have to be of the same length float[][] samples; samples=new float[6][];//defines # of arrays samples[2]=new float[6]; samples[5]=new float[101]; Not required to define all arrays

int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } }; //Three arrays //First array has 3 elements //Second array has 2 elements //Third array has 5 elements

long[][] primes = new long[20][]; primes[2] = new long[30]; System.out.println(primes.length); //Number of arrays System.out.println(primes[2].length);//Number of elements in the second array OUTPUT: 20 30

class unevenExample { public static void main( String[] arg ) { int[][] uneven = { { 1, 9, 4 }, { 0, 2}, { 0, 1, 2, 3, 4 } }; for ( int row=0; row < uneven.length; row++ ) { System.out.print("Row " + row + ": "); for ( int col=0; col < uneven[row].length; col++ ) System.out.print( uneven[row][col] + " "); System.out.println(); }

Row 0: Row 1: 0 2 Row 2:

A farmer has 10 farms of beans each in 5 countries, and each farm has 30 fields! Three-dimensional array long[][][] beans=new long[5][10][30]; //beans[country][farm][fields]

Same features apply to multi-dimensional arrays as those of 2 dimensional arrays long beans=new long[3][][];//3 countries beans[0]=new long[4][];//First country has 4 farms beans[0][4]=new long[10]; //Each farm in first country has 10 fields