public class IntList
extends java.lang.Object
| Constructor and Description |
|---|
IntList()
Initializes an empty list.
The size is zero, and the first entry is null. |
| Modifier and Type | Method and Description |
|---|---|
void |
append(int data)
Append a new entry at the back of this list.
This method is like a combination of get() and prepend(). Similarly to get(), you can loop a "current" variable until it contains the address of the final list entry. |
void |
bump(int index)
Increments the data at a particular entry in this list
You can implement this method with a call to get() followed by a call to set(). |
static IntList |
dupList(int size,
int data)
Like zeros and ones, but the caller chooses the initial value.
For example, dupList(5, 2) should return the list [2, 2, 2, 2, 2] |
boolean |
equals(IntList other)
Returns true if this list is identical to some other list.
The lists must be non-null, the same size, and contain identical values at each index. |
int |
findIndex(int data)
Finds the index of the first entry containing the specified value.
If the value does not exist in the list, return the size of the list. This should be similar to get() - you can use a reference variable to store the address of the current list entry, and then loop through the list by calling current.getNext(). |
int |
get(int index)
Gets the data at a particular entry in this list.
For example, if myList is [1, 2, 5, 3] then myList.get(0) returns 1, and myList.get(2) returns 5. |
int |
getLength()
Gets the length of this list.
For example, if myList is [1, 2, 5, 3] then myList.getLength() is 4. |
static void |
histogram(int numSides,
int numRolls)
Generate a histogram for many rolls of a many-sided die.
For example, roll a 100-sided die 10,000,000 times. |
void |
insert(int index,
int data)
Insert a new entry anywhere in the list.
For example, with input parameters index = 3, data = 7, the list [1, 4, 2, 6, 10] should change to [1, 4, 2, 7, 6, 10] |
static IntList |
ones(int size)
Create a new list with each entry initialized to one.
|
void |
plot()
"Plots" the list in the console, using the '?' character.
The number of '*'s in each row is given by the corresponding entry. For example, the list [2, 7, 1, 5] should result in the following console output: ## ####### # ##### where '#' would be replaced by the solid block character ('?'). (Ignore the "br" tags, these are just for formatting the documentation) |
IntList |
plus(IntList other)
Compute the entry-wise sum of the current list with another list.
For example, adding the lists [0, 1, 2, 3] [1, 5, 5, 3] Should return the list [1, 6, 7, 6] You can do this with a call to the constructor, followed by a loop with calls to get() and prepend(). |
IntList |
prepend(int data)
Inserts a new entry at the front of this list.
For example, if myList is [1, 2, 5, 3] after calling myList.prepend(10), myList will have changed to [10, 1, 2, 5, 3] |
int |
prod()
Compute the product of the values stored in the list
|
static IntList |
range(int start,
int stop)
Create a new list with each entries in the specified range
For example, input parameters start = 3, stop = 7 should return [3, 4, 5, 6, 7] |
static int |
rollBiasedDie(int numSides)
Perform one "roll" of a biased many-sided die.
|
static int |
rollFairDie(int numSides)
Perform one "roll" of a fair many-sided die.
|
void |
set(int index,
int newData)
Sets the data at a particular entry in this list.
For example, if myList is [1, 2, 6, 3, 4] then after calling myList.set(3, 9) it will be changed to [1, 2, 6, 9, 4] |
int |
sum()
Computes the sum of the values stored in the list
Use a loop, getLength(), and get() |
java.lang.String |
toString()
Returns a String representation of this list.
For example, "[1, 2, 6, 3]" |
static IntList |
zeros(int size)
Create a new list with each entry initialized to zero.
|
public IntList()
public int getLength()
public IntList prepend(int data)
data - the int value to be stored in the new entrypublic int get(int index)
index - the position in this list containing the datapublic java.lang.String toString()
toString in class java.lang.Objectpublic int sum()
public int prod()
public void plot()
public boolean equals(IntList other)
other - The other IntList to compare withpublic static IntList zeros(int size)
size - the size of the new listpublic static IntList ones(int size)
size - the size of the new listpublic static IntList dupList(int size, int data)
size - the size of the new listdata - the constant value to be used at every entrypublic static IntList range(int start, int stop)
start - the first entry in the liststop - the last entry in the listpublic void set(int index,
int newData)
index - the position in this list to overwritenewData - the new value to be storedpublic void bump(int index)
index - the position in this list to incrementpublic IntList plus(IntList other)
other - the other list to be added to the current onepublic int findIndex(int data)
data - the value to search forpublic void append(int data)
data - the int value to be stored in the new entrypublic void insert(int index,
int data)
index - the position at which to insert the new entrydata - the value to insertpublic static int rollFairDie(int numSides)
numSides - the number of "sides" on the "die" being rolledpublic static int rollBiasedDie(int numSides)
numSides - the number of "sides" on the "die" being rolledpublic static void histogram(int numSides,
int numRolls)