Created
November 27, 2016 07:05
-
-
Save bhnascar/0f0ae25c0ce2565c13b8e6120e9dfd9b to your computer and use it in GitHub Desktop.
ArrayList exercise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayListTest | |
{ | |
public static void main(String[] args) | |
{ | |
MyArrayList list = new MyArrayList(5); | |
for (int i = 0; i < 1000; i++) { | |
list.add(i); | |
} | |
// Check size. | |
if (list.size() != 1000) { | |
System.out.println("|size| was " + list.size() + | |
" but it should be 1000. Check your |add| or |size| method."); | |
return; | |
} | |
// Check add. | |
for (int i = 0; i < 1000; i++) { | |
if (!list.contains(i)) { | |
System.out.println("|contains| returned false for " + i + | |
" but it should be true. Check your |add| or |contains| method."); | |
return; | |
} | |
} | |
// Remove even numbers. | |
for (int i = 0; i < list.size(); i++) { | |
list.remove(i); | |
} | |
// Check remove. | |
for (int i = 0; i < 1000; i++) { | |
boolean result = list.contains(i); | |
if (i % 2 == 0) { | |
if (result != false) { | |
System.out.println("|contains| returned true for " + i + | |
" but it should be false. Check your |remove| method."); | |
return; | |
} | |
} | |
else { | |
if (result != true) { | |
System.out.println("|contains| returned false for " + i + | |
" but it should be true. Check your |remove| method."); | |
return; | |
} | |
} | |
} | |
// Check size. | |
if (list.size() != 500) { | |
System.out.println("|size| was " + list.size() + | |
" but it should be 500."); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* MyArrayList is a wrapper around a simple int[] array | |
* that makes it automatically resize when you want to | |
* add new items and there isn't enough space. This is | |
* basically how the Java ArrayList class works, except | |
* that the Java ArrayList class can contain any type of | |
* object, not just ints. | |
* | |
* The point of this exercise is to illustrate how you can | |
* add more functionality to something like an array, and | |
* make it easier to use. | |
*/ | |
public class MyArrayList | |
{ | |
/* The internal array that will actually contain the | |
* items of MyArrayList. This internal array should be | |
* resized as necessary. */ | |
int[] internalArr; | |
/* Creates an internal array with the size |size|. */ | |
public MyArrayList(int size) | |
{ | |
} | |
/* Adds an item to the internal array. If the internal | |
* array is already full, then resize it by 2x. Create | |
* a new array that's twice as long and copy all the old | |
* items into the new array. Then set the new array to | |
* be the internal array. Then add the new item to the | |
* internal array. */ | |
public void add(int item) | |
{ | |
} | |
/* Returns the item at the given index in the internal | |
* array. What happens if the user asks for an item | |
* at an invalid index? For example, a negative index, | |
* or an index >= the length of the internal array? */ | |
public int get(int index) | |
{ | |
return 0; | |
} | |
/* Removes the item from the given index in the internal | |
* array. Returns the removed item. | |
* | |
* When you remove an item, you must shift everything | |
* after it down so that there is no "empty" slot in | |
* the array. | |
* | |
* REMIND ME TO TELL YOU SOME FINE POINTS ABOUT THE | |
* SHIFTING AND FOR LOOPS, IN CASE I FORGET. | |
**/ | |
public int remove(int item) | |
{ | |
return 0; | |
} | |
/* Returns true if the internal array contains the given | |
* item, false otherwise. */ | |
public boolean contains(int item) | |
{ | |
return false; | |
} | |
/* Returns the index of the given item, if it exists in the | |
* internal array. Otherwise, returns -1. */ | |
public int indexOf(int item) | |
{ | |
return 0; | |
} | |
/* Returns the size of the internal array. How can you | |
* do this without a for-loop? | |
* | |
* The reason you might want to do this without a for-loop | |
* is because a for-loop is slow. A for-loop is like | |
* manually counting the number of items in the internal | |
* array, starting from 0. Every time |size()| is called, | |
* you have to start the counting all over again, from 0. | |
* Is there some way to "save" your counting result from | |
* last time so you don't have to start over? | |
*/ | |
public int size() | |
{ | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment