Sign in to follow this  
cdrww

[java] Simple array with generics?

Recommended Posts

Hey dudes! I understand how to use generics when you use a generic array, but i still havent found much about how to use a simple array with generics. Anyone got a link to a tutorial or would like to explain? would be real helpful :D

Share this post


Link to post
Share on other sites
By any chance do you mean the arrayList collection class when you said array the first time?

Share this post


Link to post
Share on other sites
Hello!

The code is:
import java.util.Arrays;

public class ObjectArrayList
{
private Object[] listArray = new Object[1];
private int lastIndex = 0;

/**
* Method for adding a new object at the last place of the list.
*
* @param o The object to be inserted.
*
*/

public void addLast(Object o)
{
if (lastIndex + 1 >= listArray.length)
expandArray();

listArray[lastIndex++] = o;
}

/**
* Method for adding a new object on the first index.
* The method is using the insert-method for achieving this.
* @param o The Object to be inserted into the list.
* @throws IndexOutOfBoundsException if the supplied
* index is greater than the size of the list.
*
*/

public void addFirst(Object o)
{
insert(o, 0);
}

/**
* Method for inserting a new object at a given index.
* Expands the internal array if neccesary.
*
* @param o The Object to be inserted into the list.
* @param index The index where the Object is going to
* be inserted.
* @throws IndexOutOfBoundsException if the supplied
* index is greater than the size of the list.
*
*/

public void insert(Object o, int index)
{
if (index < 0 || index > lastIndex)
throw new IndexOutOfBoundsException();

if (lastIndex + 1 >= listArray.length)
expandArray();

for (int i = lastIndex; i >= index; i--)
listArray[i + 1] = listArray[i];

listArray[index] = o;
lastIndex++;
}

/**
* Method for retrieving an element at a given index.
*
* @param index the index of the element to retrieve.
* @throws IndexOutOfBoundsException if the given index is
* less than zero or larger than the size of the array.
* @return Object the selected object.
*
*/

public Object get(int index)
{
if (index < 0 || index >= lastIndex)
throw new IndexOutOfBoundsException();

return listArray[index];
}

/**
* Method for sorting the array, using the Arrays.sort()-method.
* The method sorts the objects in the interval between zero and
* lastIndex to avoid NullPointerExceptions.
*
*/

public void sort()
{
Arrays.sort(listArray, 0, lastIndex);
}

/**
* Method for removing an element at a given index.
*
* @param index the index of the element to remove.
* @throws IndexOutOfBoundsException if the given index is
* less than zero or larger than the size of the array.
*
*/
public void delete(int index)
{
if (index < 0 || index >= lastIndex)
throw new IndexOutOfBoundsException();

for (int i = index; i < lastIndex; i++)
listArray[i] = listArray[i + 1];
lastIndex--;

}

private void expandArray()
{
Object[] newArray = new Object[listArray.length*2];
for (int i = 0; i < listArray.length; i++)
newArray[i] = listArray[i];

listArray = newArray;
}

/**
* Method for retreiving a textual representation of the list.
*
* @return String The textual representation of the list.
*
*/

public String toString()
{
String result = "[";
for (int i = 0; i < lastIndex; i++)
{
result += listArray[i];

if (i < lastIndex - 1)
result += ", ";
}

result += "]";

return result;
}

/**
* Method for retreiving the size of the list, using the lastIndex variable
*
* @return int The number of elements in the list.
*/

public int size()
{
return lastIndex;
}
}

thx for the fast awnser guys:D

Edit: My assignment is to make the list generic without changing the array. If i could change the array it wouldnt be that hard but now that i cant i´m stuck..

thanks again:)

Share this post


Link to post
Share on other sites
You should research how generics are implemented in Java. A simple overview is that it is done with type erasure, the compiler verifies that the code is type safe, then it replaces the code with similar code that deals with Object instances, and inserts casts where appropriate.

Another interesting idea is that you cannot implement this class using a "generic array", because you cannot allocate an array of generic type in Java. This is because of how arrays are implemented. You can assign an array of type Foo to any supertype of Foo. However, this introduces a potential for error:

public static void example()
{
String [] strings = new String[1];
Object [] objects = strings;
objects[0] = new Integer(1);
}


This will compile, but if you try run it it will throw an ArrayStoreException. This is because the types stored in the array must be Strings, so even though we can read the elements in the "objects" array, we cannot write non-strings into this array. Consider the following illegal java code:

public static <T> void example()
{
T [] generic = new T[1];
Object [] objects = generic;
objects[0] = new String();
objects[0] = new Integer(1);
}


If this were to compile, at runtime the T type would be erased, and this would result in the type of "generic" to be Object[], which means that the array no longer can throw the ArrayStoreException when a non-T type is written to it.

In summary, the type ArrayList in Java is actually implemented much the same way as your code is going to be. What you need to think about is the invariants your class will impose. For example, if you only ever write T instances into the array, even though the array is of type object then it is safe to cast any element of the array to type T.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
This will compile, but if you try run it it will throw an ArrayStoreException.

This, btw, is one of my least-favoured "features" of Java, and I'd strip it from the JLS given the chance. Do not rely on it in your code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this