CSC212 Implementing the Python List in Java: Version 1
--D. Thiebaut (talk) 11:12, 16 September 2014 (EDT)
Contents
To Be Done in a Group
Starting Point
- Refresh your memory about how Python Lists work (Python.org page on Lists)
- Come up with a Java class that would use an array (or several), and that would implement the same functionality as a Python list. In particular, we are interested in:
- Creating a new list object (we're interested in numbers)
- Appending a collection of integers to it
- Displaying the contents of the object.
Output
- List of methods needed
- List of member variables/fields
- Which variables/fields should be private, which should be public?
- Sketch the code for each method
- Sketch how to fully test the class created
- Write down potential problem areas
- Make a note of the limitations discovered
- Anything that we need to learn in order to make it work smoothly/better?
Possible Solution
public class PythonList {
int[] array = null;
int maxdim;
int end = 0;
PythonList(int m) {
maxdim = m;
end = 0;
array = new int[maxdim];
}
PythonList() {
this(100);
}
public void append(int n) {
if (end <= array.length - 1) {
array[end++] = n;
return;
}
int[] temp = new int[maxdim * 2];
for (int i = 0; i < end; i++)
temp[i] = array[i];
array = temp;
array[end++] = n;
maxdim = maxdim * 2;
}
public int length() {
return end;
}
public int at(int index) {
if (index >= 0 && index < end)
return array[index];
// this is bad... we'll fix it later...
else
return -1;
}
public static void main(String[] args) {
// --- Test List ---
PythonList L = new PythonList(5);
for (int i = 0; i < 11; i++)
L.append(i);
for (int i = 0; i < L.length(); i++)
System.out.println(L.at(i));
}
}
Possible Solution (With Javadoc)
/**
* PythonList: a first approach to creating a data structure Limitations: no
* exceptions treated.
*
* @author thiebaut
*
*/
public class PythonList {
int[] array = null;
int maxdim;
int end = 0;
/**
* Constructor.
*
* @param m
* Integer that defines the initial size of the list.
*/
PythonList(int m) {
maxdim = m;
end = 0;
array = new int[maxdim];
}
/**
* Constructor. No size specified, we default it to 100.
*/
PythonList() {
this(100);
}
/**
* appends an int to the end of the list.
*
* @param n
*/
public void append(int n) {
if (end <= array.length - 1) {
array[end++] = n;
return;
}
int[] temp = new int[maxdim * 2];
for (int i = 0; i < end; i++)
temp[i] = array[i];
array = temp;
array[end++] = n;
maxdim = maxdim * 2;
}
/**
* returns the length of the list.
*
* @return an int, which is the length of the list, 0 if empty.
*/
public int length() {
return end;
}
/**
* returns element at index specified.
*
* @param index
* the integer index where item is located.
* @return the item at the specified index, or -1 if invalid index.
*/
public int at(int index) {
if (index >= 0 && index < end)
return array[index];
// this is bad... we'll fix it later...
else
return -1;
}
/**
* main program. Tests an object of type PythonList.
*
* @param args
*/
public static void main(String[] args) {
// --- Test List ---
PythonList L = new PythonList(5);
for (int i = 0; i < 11; i++)
L.append(i);
for (int i = 0; i < L.length(); i++)
System.out.println(L.at(i));
}
}