[PDF] ArrayLists - cscolostateedu



Previous PDF Next PDF
















[PDF] exemple arraylist java

[PDF] créer une arraylist java

[PDF] constructeur arraylist java

[PDF] arraylist<int>

[PDF] droit d'arrestation article

[PDF] interpellation police a domicile

[PDF] arrestation enquête préliminaire

[PDF] arrestation procédure pénale

[PDF] heure légale arrestation

[PDF] enquete preliminaire et garde a vue

[PDF] est ce que la police a le droit de rentrer chez mo

[PDF] arrestation citoyenne france

[PDF] article interpellation preliminaire

[PDF] droit lors d une arrestation

[PDF] pouvoir d'arrestation du citoyen

ArrayLists - cscolostateedu

ArrayLists

Using arrays to store data

"Arrays store multiple values of the same type "Individual values are referred to by index "Convenient to use

‰a[i] = x;

‰x = a[i];

"Limitations of arrays

‰Fixed in size

‰GRQ P LQGLŃMPH ROLŃO YMOXHV MUH YMOLG

Using arrays to store data

int[] nums = new int[100]; int size = 0; "We often need to store an unknown number of values. ‰Arrays can be used for this, but we must count the values. ‰Only the values at indexes [0, size - 1] are relevant. ‰Need to resize the array to store additional values index 0 1 2 3 4 5 6 ... 98 99 value 17 932085 -3 100 3 0 0 ... 0 0 size 5

Solving the fixed size ² make a Class

public Class IntArray { private int[] data; public IntArray (int initialSize) { data = new int[initialSize]; public int get (int index) return ((index < data.length) ? data[index] : 0); public void set (int index, int val) { if (index >= data.length) growArray(index); data[index] = val;

Solving the fixed size ² cont

public Class IntArray { private int[] data; private void growArray (int index) { int newSize = ??; // how do we determine this? int[] newData = new int[newSize]; for (int i = 0; i < data.length; i++) newData[i] = data[i]; data = newData;

Using our new class

IntArray a = new IntArray(10);

int x = a.get(i); // instead of x = a[i]; a.set(i, x); // instead of a[i] = x;

Solving the validity of the entries

"Keep track of number of valid entries ‰Track number of entries in instance variable size ‰Only values at indices 0 <= i < size are valid

Other possible operations

public static void add(int[] data, int size, int value) public static void remove(int[] data, int size, int index) public static void find(int[] data, int size, int value) public static void print(int[] data, int size) "We could implement these operations as methods that accept an array and its size along with other parameters. ‰But since the behavior and data are so closely related, it makes more sense to put them together into a class. ‰Objects of such a class can store an array of elements and a size, and can have methods for manipulating the elements. "Promotes abstraction (hides details of how the list works) Lists "list: a collection storing an ordered sequence of elements, each accessible by a 0-based index ‰a list has a size (number of elements that have been added) ‰elements can be added to the back, or elsewhere

Thought experiment...

"A class that implements a list using an int[]

‰We'll call it ArrayIntList

‰behavior:

"add(value), add(index, value) "get(index), set(index, value) "size() "remove(index) "indexOf(value) ‰The list's size will be the number of elements in the list

Using ArrayIntList

"construction

ArrayIntList list = new ArrayIntList();

"storing a value retrieving a value // add at end list.add(42); int n = list.get(0); // add in middle list.add(10,42); "searching for the value 27 if (list.indexOf(27) >= 0) { ... }

Implementing add

"Add to end of list is easy; just store element and increase size public void add(int value) { list[size] = value; size++;

‰list.add(42);

index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 0 0 0 0 size 6 index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 42 0 0 0 size 7

Implementing add (2)

"Adding to the middle or front is harder ‰must shift nearby elements to make room for the new value

‰list.add(3, 42);

‰Note: The order in which you traverse the array matters! index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 7 5 12 0 0 0 0 size 6 index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 42 7 5 12 0 0 0 size 7

Implementing add (2)

public void add(int index, int value) { for (int i = size; i > index; i--) { list[i] = list[i - 1]; list[index] = value; size++; list.add(3, 42); index 0 1 2 3 4 5 6 7 8 9 value 3 8 9 42 7 5 12 0 0 0 size 7

Implementing remove

quotesdbs_dbs2.pdfusesText_2