ArrayLists: Difference between revisions
From charlesreid1
(Created page with "Array-based list data structure. ==Java== ===Built-in ArrayList type=== Java Collections uses built-in array-based list structure, ArrayList<>. Link: https://docs.oracle.co...") |
(→Python) |
||
| Line 88: | Line 88: | ||
The essence of this class is that it is a dynamically sizable, array-based structure. In the Goodrich Python book, the array-based list, emulating the built-in Python list type, is covered in Chapter 3 - the first data structure you learn about in Python is a list. It's a wonderfully simple data container. The Java version of Goodrich doesn't get around to implementing your own array-based list type until Chapter 6! | The essence of this class is that it is a dynamically sizable, array-based structure. In the Goodrich Python book, the array-based list, emulating the built-in Python list type, is covered in Chapter 3 - the first data structure you learn about in Python is a list. It's a wonderfully simple data container. The Java version of Goodrich doesn't get around to implementing your own array-based list type until Chapter 6! | ||
Here is an implementation of a few of the concepts from Chapter 3 in the form of the DynamicArray class, which emulates the built-in, array-based list type in its simple design: https://charlesreid1.com:3000/cs/python/src/master/arrays/DynamicArray.py | |||
This implementation uses the ctype module to access a low-level array of Python Objects. | |||
Revision as of 00:54, 3 June 2017
Array-based list data structure.
Java
Built-in ArrayList type
Java Collections uses built-in array-based list structure, ArrayList<>. Link: https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
Bringing the Python List to Java
Here is an implementation of a Python list in Java, which is pretty close in style to the Python list, which works transparently with any data so long as they have matching types.
https://charlesreid1.com:3000/cs/java/src/master/arrays/python-list/PythonList.java
public class PythonList {
public static void main(String[] args) {
PythonList p = new PythonList();
Random r = new Random();
int n = 50;
for(int i=0; i<100; i++ ) {
p.append(r.nextInt(6)+1);
System.out.println(p);
}
System.out.println(p);
}
///////////////////////////////////////////////////
final private int INITCAP = 10;
private Object[] data;
public int length;
public PythonList() {
length = 0;
data = new Object[INITCAP];
}
public Object get(int ix) {
return data[ix];
}
public void append(Object o) {
if(data.length==length) {
resize(data.length*2);
}
data[length] = o;
length++;
}
public void remove(int rmi) {
if(rmi>=length) {
throw new ArrayIndexOutOfBoundsException("Error accessing index "+rmi);
}
data[rmi] = null;
for(int i=rmi; i<length; i++) {
data[i] = data[i+1];
}
length--;
if(length<(data.length/4)) {
resize(data.length/2);
}
}
private void resize(int newcap) {
Object[] newarr = new Object[newcap];
for(int i=0; i<length; i++ ) {
newarr[i] = data[i];
}
data = newarr;
}
public String toString() {
return Arrays.toString(data);
}
}
Python
The essence of this class is that it is a dynamically sizable, array-based structure. In the Goodrich Python book, the array-based list, emulating the built-in Python list type, is covered in Chapter 3 - the first data structure you learn about in Python is a list. It's a wonderfully simple data container. The Java version of Goodrich doesn't get around to implementing your own array-based list type until Chapter 6!
Here is an implementation of a few of the concepts from Chapter 3 in the form of the DynamicArray class, which emulates the built-in, array-based list type in its simple design: https://charlesreid1.com:3000/cs/python/src/master/arrays/DynamicArray.py
This implementation uses the ctype module to access a low-level array of Python Objects.