Java Array Object Chuen-Liang Chen Department of Computer Science

Slides:



Advertisements
Similar presentations
Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Advertisements

A pointer is the memory address of a variable. A memory address is a physical location within a system’s memory space. A pointer variable is variable used.
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Introduction to Programming Lecture 39. Copy Constructor.
Analysis of programs with pointers. Simple example What are the dependences in this program? Problem: just looking at variable names will not give you.
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
CIS 101: Computer Programming and Problem Solving Lecture10 Usman Roshan Department of Computer Science NJIT.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Memory organization and usage A computer’s memory is, at its lowest level, composed of binary digits (bits). The memory is organized into bytes, which.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
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.
21-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Multidimensional Arrays Lecture 20 4/3/2002.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 3 – August 28, 2001.
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.
Data Structure & Algorithms
Array contiguous memory locations that have the same name and type. Note: an array may contain primitive data BUT an array is a data structure a collection.
13/10/2016CS150 Introduction to Computer Science 1 Multidimensional Arrays  Arrays can have more than one column  Two dimensional arrays have two columns.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Object Oriented Programming Lecture 2: BallWorld.
Memory Management in Java Mr. Gerb Computer Science 4.
Java Programming Language Lecture27- An Introduction.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
C Programming Lecture 15 Two Dimensional Arrays. Two-Dimensional Arrays b The C language allows arrays of any type, including arrays of arrays. With two.
Java Interfaces CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (see Chapter 9 of.
Array in C# Array in C# RIHS Arshad Khan
Sections 10.1 – 10.4 Introduction to Arrays
Chapter 6 – Data Types CSCE 343.
Two-Dimensional Arrays
Array, Strings and Vectors
Arrays.
Arrays 2/4 By Pius Nyaanga.
C++ Arrays.
Objects First with Java CITS1001
Pointers and References
Dynamic Memory Allocation Reference Variables
Dynamic Memory Allocation
بردارها و ماتريس ها ساختمان داده ها
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
int [] scores = new int [10];
Lecture 18 Arrays and Pointer Arithmetic
Arrays and Collections
Introduction To Programming Information Technology , 1’st Semester
Arrays .
Multidimensional Arrays
prepared by Senem Kumova Metin modified by İlker Korkmaz
Cs212: Data Structures Computer Science Department Lecture 2: Arrays.
CS111 Computer Programming
Arrays Week 2.
Dr Tripty Singh Arrays.
INC 161 , CPE 100 Computer Programming
Arrays in Java.
A Wide Array of Possibilities
C Programming Lecture-8 Pointers and Memory Management
Java Programming Language
C Programming Pointers
Arrays.
Arrays.
Chapter 9: Pointers and String
Intermediate Code Generation
Arrays 2-May-19.
SYMBOL TABLE Chuen-Liang Chen Department of Computer Science
RUN-TIME STORAGE Chuen-Liang Chen Department of Computer Science
Comp 208 Computers in Engineering Yi Lin Fall, 2005
Pointers and References
ENERGY 211 / CME 211 Lecture 11 October 15, 2008.
LINEAR DATA STRUCTURES
Introduction to Pointers
Presentation transcript:

Java Array Object Chuen-Liang Chen Department of Computer Science & Information Engineering National Taiwan University

Fortran Array & C Array Fortran – a contiguous space column major declaration & allocation – INTEGER a(2,3) memory a(i, j) is located at – S + (j -1) * 2 + (i - 1) C, C++ – a contiguous space row major declaration & allocation – int a[2][3]; memory a[i][j] is located at – S + i * 3 + j a(1,1) a(2,1) a(1,2) a(2,2) a(1,3) a(2,3) S a[0][0] a[0][1] a[0][2] a[1][0] a[1][1] a[1][2] S

Java Array an object; a special instance of subclass of java.lang.Object an array of 3 int’s an array of 3 shorts an array of 3 (array of int)’s an array of 3 (array of short)’s int[ ] length 3 ?[0] ?[1] ?[2] int[ ][ ] length 3 ?[0] ?[1] ?[2] short[ ] length 3 ?[0] ?[1] ?[2] short[ ][ ] length 3 ?[0] ?[1] ?[2]

Array Variable Declaration class variable declaration class X { int i; short j; } X x1; 32 bits a pointer; a reference to an object of X, to an object of subclass of X array variable declaration int[ ] a; OR int a[ ]; 32 bits a pointer; a reference to an arbitrary length array of int note: both of int[ ] and int[ ][ ] are direct subclasses of java.lang.Object int[ ][ ] is not the subclass of int[ ], and vise versa

Array Creation Expression ordinary instance creation class X { int i; short j; } x1 = new X(); array instance creation int[ ] a; a = new int[3]; x1 X i j int[ ] length 3 ?[0] ?[1] ?[2] a

Array Access Expression access of instance variable instance variables x1.i, x1.j initial value = 0 access of array element instance variables a.length (read only) initial value = 3 a[0], a[1], a[2] initial value = 0 x1 X i j int[ ] length 3 ?[0] ?[1] ?[2] a

Operation of Array Variable assignment of class variable x1.i = 10; X x2 = x1; // now, x2.i == 10 assignment of array variable a[0] = 10; int[ ] b = a; // now, b[0] == 10 x1 X i 10 j x2 int[ ] length 3 ?[0] 10 ?[1] ?[2] a b

Array Initializer int[ ] a = {10, 20, 30}; 4 steps array variable declaration empty array instance creation (length = 3) array elements initialization assignment of array variable

Array of Object (1/3) class Y { int i; Y(int p) { i = p;} void m() { … } } class Z extends Y { int j; Z(int p, int q) { super(p); j = q;} } Y[ ] c; declare an array variable, named c 32 bits; a pointer, a reference to an arbitrary length array element type: Y c

Array of Object (2/3) c = new Y[3]; create a 3 elements array instance each element 32 bits; a pointer, a reference to an object of Y, or an object of subclass of Y initial value = null Y[ ] length 3 ?[0] null ?[1] ?[2] c

Array of Object (3/3) c[0] = new Y(10); c[1] = new Z(20, 30); create a Y instance and a Z instance c[1].i c[1].m() access of field and method precedence of field selection, array access, method invocation are the same (15L) Y[ ] length 3 ?[0] ?[1] ?[2] null c Y i 10 Z i 20 j 30

Array of Array (1/3) int[ ][ ] d; declare an array variable, named d 32 bits; a pointer, a reference to an arbitrary length array element type: int[ ] Y[ ][ ] e; declare an array variable, named e element type: Y[ ] d e

Array of Array (2/3) d = new int[2][1]; d[1][0] = 10; d = new int[2][ ]; d[1] = new int[2]; int[ ][ ] length 2 ?[0] ?[1] d int[ ] 1 10 int[ ][ ] length 2 ?[0] null ?[1] d int[ ]

Array of Array (3/3) e = new Y[2][1]; e[1][0] = new Z(20,30); e = new Y[2][ ]; e[1] = new Y[2]; Y[ ][ ] length 2 ?[0] ?[1] e Y[ ] 1 null Z i 20 j 30 Y[ ][ ] length 2 ?[0] null ?[1] e Y[ ]