At the hardware level, most computer architectures provide a mechanism for creating and using 1-D arrays. A one-dimensional array, is composed of multiple sequential elements stored in contiguous bytes of memory. The entire contents of an array are identified by a single name. It holds a fixed number of values of a single type. The length of an array is established and fixed and the array is created.
Python doesn't have a native array data structure. but it has a more general list structure. The list can grow and shrink during execution as elements are added or removed while the size of an array cannot be changed. However, the tradeoff here is that it needs more space, which allows for quick and easy expansion as new items are added to the list.
Another difference is that while the array only allows single type, the list in Python allows multiple types:
list1 = ['shirley','2014']
list2 = ['dora','shine']
list3 = [2011, 2015]
Note that the list is also implemented using an array structure to store the items. When the
list() constructor is called, an array structure is created. The array is initially created bigger than needed, leaving the capacity for future expansion. If the number of elements added to the list exceeds the capacity (which is larger than the initial size) of the list, a new array with a larger capacity is created and an array copy is followed.
In Java, the best analogy should be ArrayList. A slight difference is that the ArrayList in Java only allows single type and initialize an array with very small capacity (10) if not predefined.
Creating an empty list:
list = []
#create a list of None with size 256
list = [None]*256
list = list()
In Java, everything is instantiated with the
new operator:
int[] array = new int[10];
List arraylist = new ArrayList();
Extending a list:
Python provides built in method to append one list to another:
list2.extend(list3)
In ArrayList in Java, that is:
arraylist.addAll(arraylistB);
Inserting items:
In Java, neither Array or ArrayList allows this operation:
list1.insert(2, "Jesse")
However, since the list is still array based, this operation requires shift the elements from index 2 to the right then insert "Jesse" to index 2.
Slicing:
Slicing is an operation that creates a new list consisting a contiguous subset of elements from the original list. References to the corresponding elements are copied and stored in the new list:
list4 = list1[startIndex(inclusive) : endIndex(exclusive)]
JDK 8 provides a similar method
subList(int fromIndex(inclusive), int toIndex(exclusive)) (
here for src):
List sublist = arrayList(1, 3);
Creating 2-D array/list:
In Python, this involves creating a list containing m lists initialized to 0:
matrix = [[] for x in range(rows of the matrix)]
Similarly, you have to at least initialize 1 dimension when creating a 2-D array.
Alternatively, you can use library
numpy:
import numpy
numpy.zeros((m, n))
Well, in Java, this is much easier:
int[][] matrix = new int[m][n];
References:
[1]. Data structures and algorithms using Python. Wiley Publishing, 2010.
[2].
www.grepcode.com
[3]. Beloved Java doc
[4].
www.codatlas.com