Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCI 3333 Data Structures Array

Similar presentations


Presentation on theme: "CSCI 3333 Data Structures Array"— Presentation transcript:

1 CSCI 3333 Data Structures Array
by Dr. Bun Yue Professor of Computer Science

2 What is an array? Wikipedia’s array data structure: “a data structure consisting of a collection of elements (values or variables), each identified by at least one array index or key.” Array in Wikipedia: There are many definitions.

3 Array Definitions Many kinds and definitions of arrays: arrays, association arrays, array data types, array classes, … E.g. Java: “An array is a container object that holds a fixed number of values of a single type.”

4 Array definitions C++: “An array is a series of elements of the same type placed in contiguous memory locations that can be individually referenced by adding an index to a unique identifier.” Perl: “An array is an ordered sequence of scalars.”

5 Array Essential concepts: A container Ordered
Elements of identical types Element identified by indices

6 Differences Among Languages
C++: int a[] = {2,4,6,8}; Java & C#: int[] a= {2,4,6,8}; = {2,4,6,8}; PHP: hash and array the same $a=array(2,4,6,8); $b=array(‘bun’=>2, ‘ben’=>4); JavaScript: object var a=new Array(2,4,6,8);

7 Arrays in various languages
Java C++ Perl PHP Javascript Nature Built-in Built-in; map Object Element Size Fixed Variable # elements Element Type Any Scalar Pointer (Arithmetic) No Yes Mainly no Index/key Non-negative integer Integer Integer or string non-negative integer

8 Arrays in Java Java has: Built-in array construct
Classes and Interfaces associated with array.

9 Java’s arrays Zero-based Fixed size Elements have the same type.
An array variable is a handle (reference) to an array.

10 RandomNumbers.java As an example, we study the program RandomNumbers.java here. Code reading: what does the code to? Any conclusion in running the program?

11 Copying array To copy all element values of b to a (assuming a and b have the same size), this is incorrect: int[] a = new int[]{1,2,3,4,5}; int[] b = new int[5]; a=b; // a and b refer to the same array. // the initial array referred by a becomes garbage.

12 Array cop Instead, use: for(int i=0; i<5; i++) { a[i]=b[i]; } or
System.arraycopy(b, 0, a, 0, 5);

13 Questions and Comments?


Download ppt "CSCI 3333 Data Structures Array"

Similar presentations


Ads by Google