Docsity

Java Data Structures: An Introduction to Arrays and Array Manipulation, Study notes of Computers and Information technologies

Toggle sidebar
bg1
1
Data Structures G5029
Kingsley Sage
Room 5C16, Pevensey III
© University of Sussex 2006
Lecture 1
Lecture 1
Introduction
–This course introduces the Java programming language …
–… and the use of data structures as a means of representing a
wide range of tasks in general computing
–Assessment is by coursework (two Java programming
exercises) and by formal examination
Resources
–On-line course notes (by Peter Williams)
–Lab sesssions
–Course web site at http://www.informatics.sussex. ac.uk/courses/dats
pf3
pf4
pf5

Partial preview of the text

Download Java Data Structures: An Introduction to Arrays and Array Manipulation and more Study notes Computers and Information technologies in PDF only on Docsity!

Data Structures G

Kingsley Sage

Room 5C16, Pevensey III

[email protected]

© University of Sussex 2006

Lecture 1

Lecture 1

 Introduction

  • This course introduces the Java programming language …
  • … and the use of data structures as a means of representing a

wide range of tasks in general computing

  • Assessment is by coursework (two Java programming

exercises) and by formal examination

 Resources

  • On-line course notes (by Peter Williams)
  • Lab sesssions
  • Course web site at http://www.informatics.sussex.ac.uk/courses/dats

Primitive data types in Java

 The simplest data structures in the Java programming

language are the built in primitive data types byte,

short, int, long, float, double, char and

boolean.

 Wrapper classes – the difference between byte and

Byte, int and Int …

 All numeric types in Java are signed (unlike C/C++).

 The expression “data structure” is usually used to refer

to more complex ways and storing and manipulating

data, such as arrays, queues, stacks etc …

 We begin by discussing the simplest, but one of the

most useful data structures, the array.

Array variables

 Array: a simple list of values referenced by an

array index. Before use, an array must be

properly initialised

int[] a; // a is a reference to an array of integers a = new int[20]; // allocates OS storage for 20 integers int[] a = new int[20]; // combines the above into one line a[2] = 173; // gives the array element index 2 the value 173 // array element 2 is the third item in the array a[i] = 34; // array element i – i must be of type byte, // short or int

Assignment

 Arrays are treated as objects in Java, and as such, obey the

same rules for equality and assignment.

 An array variable is a reference variable (like a pointer variable in

C/C++). It stores information about where to locate the array

elements. All array variables required the same amount of

memory storage.

int[] a = new int[20]; int[] b; // Just a reference variable … b = a; // a[x] and b[x] refer to the same data – there is only one // array. Any change to b[x] will change a[x] identically // and vice versa

Assignment

 Now there are 2 arrays and you can change one without affecting

the other.

int[] a = new int[20]; b = new int[a.length]; for (int i = 0; i < a.length; i++) { b[i] = a[i]; }

 To actually copy an array takes a bit more work …

Equality

 Equality behaves in a similar manner …

 a == b is true if and only if the array reference variables a and b

stored the same address.

 More commonly, you might wish to check whether a and b are of

the same length and have the same contents. In that case, you

need to write your own simple method …

Arrays as parameters

int[] a = new int[20]; a[0] = 3; foo(a); void foo(int[] x) { x[1] = 4; }

 Array parameters can be used as parameters to methods.

 When an array is passed to a methods, only the reference is passed to

the method, which makes for efficient passing of arrays to methods

(what’s the alternative?)

 Method has access to the elements of the array, via the reference, which

means that they can be changed as the method chooses.

 The reference itself will not be changed outside the method.

Array access and memory

allocation

 One of the principal reasons why arrays are used so widely is that

their elements can be accessed in constant time.

 This means that it takes the same time to access a[1] is a[10].

 This is because the address of a[x] can be determined

arithmetically by adding a suitable offset to the machine address

of the head of the array.

 This relies on the fact that the elements of the array are stored in

a contiguous block of memory.

 This has advantages and disadvantages. Arrays can also be

allocated dynamically (size is only known at run time rather than

compile time).

 Dynamically sized arrays are implemented as Vectors in Java

(like C/C++)

Next time …

 The first of our more complex and useful data

structures

– The stack