ArrayLists
[Pages:26]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 Don't indicate which values are valid
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 ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- powershell quick reference security and compliance center v1 0
- arraylist github pages
- java syntax examples
- how to remove elements from arraylist in specific index range in java
- big o arraylist carnegie mellon university
- pnp powershell admin scripts set item level permission
- data structures and advanced programming pomona
- remove object method example
- powershell basic cheat sheet rambling cookie monster
- arraylist multidimensional arrays department of computer science and