ArrayLists: Difference between revisions
From charlesreid1
(→Flags) |
(→Python) |
||
| Line 93: | Line 93: | ||
This implementation uses the ctype module to access a low-level array of Python Objects. | This implementation uses the ctype module to access a low-level array of Python Objects. | ||
<pre> | |||
import ctypes | |||
class DynamicArray: | |||
""" | |||
A dynamic array class that works like a list. | |||
""" | |||
.........snip............ | |||
def _resize(self,c): | |||
""" Resize to capacity c (private)""" | |||
print("Resizing array: {0:4d} elements, {1:5d} capacity".format(self._n,c)) | |||
B = self._make_array(c) # bigger array | |||
for k in range(self._n): | |||
B[k] = self._A[k] # copy existing elements only | |||
self._A = B # forget the old array | |||
self._capacity = c # update capacity | |||
def _make_array(self,n): | |||
"""Make the internal array (private)""" | |||
return (n*ctypes.py_object)() | |||
</pre> | |||
==Flags== | ==Flags== | ||
Revision as of 01:34, 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.
import ctypes
class DynamicArray:
"""
A dynamic array class that works like a list.
"""
.........snip............
def _resize(self,c):
""" Resize to capacity c (private)"""
print("Resizing array: {0:4d} elements, {1:5d} capacity".format(self._n,c))
B = self._make_array(c) # bigger array
for k in range(self._n):
B[k] = self._A[k] # copy existing elements only
self._A = B # forget the old array
self._capacity = c # update capacity
def _make_array(self,n):
"""Make the internal array (private)"""
return (n*ctypes.py_object)()
Flags
| Data Structures Part of Computer Science Notes
This is the staging ground for computer science notes as part of the 2017 CS Study Plan.
Classes of data structures: Abstract Data Types Array-based and Link-based memory management: ArrayLists and Linked Lists Algorithmic Analysis of Data Structures: Algorithmic Analysis of Data Structures Advanced data structures: Advanced Data Structures
|
| Arrays Part of Computer Science Notes
Series on Data Structures Python: Arrays/Python · Arrays/Python/Sizeof · Arrays/Python/AppendCost · Arrays/Python/CaesarCipher · Arrays/Python/CompactArrays · Arrays/Python/DynamicArray Java: Arrays/Java · Arrays/Java/CaesarCipher · Arrays/Java/FisherYates · Arrays/Java/PythonList · Arrays/Java/Repeatedly_Remove Categories: Category:Python Arrays
|
| Stacks and Queues Part of Computer Science Notes
Series on Data Structures
Stacks and Queues: Python StacksQueues/Python · StacksQueues/Python/ArrayStack · StacksQueues/Python/ArrayQueue · StacksQueues/Python/ArrayDeque StacksQueues/Python/LinkedStack
Stacks and Queues: Java StacksQueues/Java · StacksQueues/Java/ArrayStack · StacksQueues/Java/ArrayQueue · StacksQueues/Java/ArrayQueueFS · StacksQueues/Java/ArrayDeque StacksQueues/Java/LinkedStack · StacksQueues/Java/LinkedQueue · StacksQueues/Java/LinkedDeque
Applications Postfix_Expressions#Stacks · StacksQueues/Subsets · StacksQueues/Subsets/Java
|
| Priority Queues and Heaps Part of Computer Science Notes
Series on Data Structures
Java: Priority Queues/Java · Priority Queues/ADT · Priority Queues/Sorted · Priority Queues/Unsorted Performance: Priority Queues/Timing and Performance Applications: Maximum Oriented Priority Queue · Priority Queues/Stack
Priority Queues/Heap · Priority Queues/Java · Priority Queues/Comparators
|
| Linked List Part of Computer Science Notes
Series on Data Structures Java: Linked Lists/Java · Linked Lists/Java/Single · Linked Lists/Java/Double · Linked Lists/Java/Circular Performance: Linked Lists/Java/Timing · Linked Lists/Java/Reverse Python: Linked Lists/Python · Linked Lists/Python/Single
|
| Trees Part of Computer Science Notes
Series on Data Structures Abstract data type: Trees/ADT Concrete implementations: Trees/LinkedTree · Trees/ArrayTree · SimpleTree
Tree Traversal Preorder traversal: Trees/Preorder Postorder traversal: Trees/Postorder In-Order traversal: Binary Trees/Inorder Breadth-First Search: BFS Breadth-First Traversal: BFT Depth-First Search: DFS Depth-First Traversal: DFT OOP Principles for Traversal: Tree Traversal/OOP · Tree Traversal/Traversal Method Template Tree operations: Trees/Operations Performance · Trees/Removal
Tree Applications Finding Minimum in Log N Time: Tree/LogN Min Search
Abstract data type: Binary Trees/ADT Concrete implementations: Binary Trees/LinkedBinTree · Binary Trees/ArrayBinTree Binary Trees/Cheat Sheet · Binary Trees/OOP · Binary Trees/Implementation Notes
|
| Search Trees Part of Computer Science Notes
Series on Data Structures
Binary Search Trees · Balanced Search Trees Trees/OOP · Search Trees/OOP · Tree Traversal/OOP · Binary Trees/Inorder
(Note that heaps are also value-sorting trees with minimums at the top. See Template:PriorityQueuesFlag and Priority Queues.)
|
| Maps and Dictionaries Part of Computer Science Notes
Series on Data Structures
Maps/Dictionaries Maps · Maps/ADT · Maps in Java · Maps/OOP · Maps/Operations and Performance Map implementations: Maps/AbstractMap · Maps/UnsortedArrayMap · Maps/SortedArrayMap Dictionary implementations: Dictionaries/LinkedDict · Dictionaries/ArrayDict
Hashes Hash Maps/OOP · Hash Maps/Operations and Performance Hash Maps/Dynamic Resizing · Hash Maps/Collision Handling with Chaining Hash functions: Hash Functions · Hash Functions/Cyclic Permutation Hash map implementations: Hash Maps/AbstractHashMap · Hash Maps/ChainedHashMap
Skip Lists · Java/ConcurrentSkipList · Java implementations: SkipList
Sets Sets · Sets/ADT · Sets in Java · Sets/OOP · Multisets
|