0

I would like to write a method which helps me to store some stuff in the generics class variable. I don't understand a concept a little bit :(

I would like to write a method which replace this simple solution:

a.myObject[0]=tracks;

and will works with others elements when I give as a parament an array index

I would like to figure out something like this method (but as a set a value). Does anybody have a clue how to do it?

E get(int i) {
    return (E) myObject[i];
}

My source code:

import java.util.Arrays;
import java.util.List;

public class GenericArray<E>{

private Object[] myObject;  

public GenericArray(int s) {
    myObject = new Object[s];
}

E get(int i) {
    return (E) myObject[i];
}


public static <Track> void main(String[] args) {

    GenericArray a = new GenericArray(100);

    List<String> tracks = Arrays.asList("One", "Two", "Three");

    PlayList play = new PlayList(tracks);

    a.myObject[0]=tracks;

    System.out.println(a.get(0));

}

}
Eran
  • 387,369
  • 54
  • 702
  • 768
Anna K
  • 1,666
  • 4
  • 23
  • 47
  • There's not really a great way to do it; generics and arrays don't mix very well. You can look at the implementation of ArrayList to see how the Java folks do it. – yshavit Jan 10 '16 at 19:18
  • Why is there a generic `` parameter in your `main` method? – Clashsoft Jan 10 '16 at 19:48

3 Answers3

1

It is not possible to create a generic array so change it to a collection. I adjusted your code a little bit for a example:

public class GenericArray<E extends Object> {

    private List<E> myObject;

    public GenericArray(int s) {
        //myObject = new E[s];//can not create a generic array
        myObject = new ArrayList<>(s);
    }

    public E get(int i) {
        return myObject.get(i);
    }

    public void set(int i, E object){
        myObject.set(i, object);
    }

    public static void main(String[] args) {

        GenericArray<Object> a = new GenericArray<>(100);

        List<String> tracks = Arrays.asList("One", "Two", "Three");

        PlayList play = new PlayList(tracks);

        a.set(0, tracks);

        System.out.println(a.get(0));

    }

}

Hm... i think it is because there was never anything added to the collection. (It has the capacity but does not have the elements). I looked int the comment about generic arrays so here is another example and this one should work:

public class GenericArray<E extends Object> {
    private E[] myObject;

    public GenericArray(Class<E> c,int s) {
        //myObject = new E[s];//can not create a generic array
        @SuppressWarnings("unchecked")
        final E[] a = (E[]) Array.newInstance(c, s);
        myObject = a;
    }

    public E get(int i) {
        return myObject[i];
    }

    public void set(int i, E object){
        myObject[i] = object;
    }

    public static void main(String[] args) {

        GenericArray<Object> a = new GenericArray<>(Object.class,100);

        List<String> tracks = Arrays.asList("One", "Two", "Three");

        PlayList play = new PlayList(tracks);

        a.set(0, tracks);

        System.out.println(a.get(0));

    }

}
  • 2
    *"It is not possible to create a generic array"* [How to create a generic array in Java?](http://stackoverflow.com/q/529085) – Tom Jan 10 '16 at 19:23
  • Sorry it should be set. I didn't know about that. Thank you for the link. @Tom – someone_somewhere Jan 10 '16 at 19:26
  • Thanks for help but I get an Error :( Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.set(ArrayList.java:426) at GenericArray.set(GenericArray.java:19) – Anna K Jan 10 '16 at 19:48
  • Look at the edit I added to the answer. That should work. – someone_somewhere Jan 10 '16 at 19:52
0

I think it's much better to use predefined collections to reduce number of mistakes and make code more clear for other developers or for you when you will return to it in a long time. Look into ArrayList collection for example:

    Object tracks = new Object();
    List<Object> myList = new ArrayList<Object>();
    myList.add(0, tracks);
    //myList.add(tracks); // or just use add to prevent IndexOutOfBoundsException
Vitaliy Borisok
  • 822
  • 3
  • 11
  • 21
0

So, you want to create a set method to assign the value for the generic array right. I hope this code will help you out... public void set(int i,Object obj){myObject[i] = obj;}