Docsity

Arrays and ArrayLists in Java: A Comprehensive Guide with Exercises, Exercises of Programming Languages

Partial preview of the text

Download Arrays and ArrayLists in Java: A Comprehensive Guide with Exercises and more Exercises Programming Languages in PDF only on Docsity!

Arrays and ArrayLists

Ananda Gunawardena

Introduction

Array is a useful and powerful aggregate datastructure presence in modern programminglanguages

Arrays allow us to store arbitrary sizedsequences of primitive values or sequences of references to objectsreferences to objects

Arrays allow easy access and manipulation tothe values/objects that they store

Arrays are indexed by a sequence of integers

classes can use arrays as instance variables tostore databases of value/references

integer Arrays

int[] A = new int[5];int[] A = new int[5];int[] A = new int[5];int[] A = new int[5];

Array of Object References

class foo() { ….}foo[ ] myFooList = new foo[N];

N-

Foo[0] Foo[1]

Foo[N-1]

myFooList

Array of Bytes

• We can create array of bytes and perhaps

return them from a method

• public byte[ ] foo(){

byte[] temp = new byte[10];byte[] temp = new byte[10]; for (int i=0;i<10;i++)

temp[i] = new Byte(i);

return temp;

Arrays

• Arrays have

fixed length

• Arrays have element of specific type or

references to

Objects

references to

Objects

• Operator [ ] is used to access array elements

data[4] =

29.95;

• Use length attribute to get array length.

  • data.length

. (Not a method!)

Copying Arrays

Copying an array reference yields a secondreference to the same array

double[]

data

new

double[10];

fill

array

double[]

prices

data;

Use

clone

to make true copy

double[] prices =

(double[])data.clone();

Cloning Arrays

Question

Write your own version of arraycopy for int arrays

public static void arraycopy(int[] from,int fromstart, int[] to, int tostart, int count) { }

Shifting and Copying

  • System.arraycopy(data, i,

data, i

1, data.length

System.arraycopy(data, i,

data, i

1, data.length

System.arraycopy(data, i,

data, i

1, data.length

System.arraycopy(data, i,

data, i

1, data.length -

i i

i i

data[i] =

x;

data[i] =

x;

data[i] =

x;

data[i] =

x;

Shifting Elements

• Shift all elements to Right by 1 starting at

index i

Shift all elements left by 1 starting at

index i (i>0)

Swapping Array Elements

Suppose you want to swap two elements in thearray, say entries with indices i and j.Assuming we are dealing with an array of ints

  • int temp = A[i]; // save a copy of A[i] in temp –

A [i] = A[j];

// copy the content of A[j] to A[i]

A [i] = A[j];

// copy the content of A[j] to A[i]

  • A[j] = temp;

// copy the content of temp to A[j]

Note that : A[i]= A[j] and A[j] = A[i] do not swap content

Exercise: Reverse an array using swaps

Partially Filled Arrays

• Array.length = maximum capacity of the array• Usually, array is partially filled• Need companion variable to keep track of current size

final int capacity

final int capacity

double[] data = new double[capacity];int s

ize

• Update

size

as array is filled:

data[size] = x;size++;

Partially Filled Arrays