0

I have a class which always holds four objects:

class Foo<E> {
    Cow<E> a, b, c, d;
}

I want to be able to iterate over them, so ideally I'd like to use an array:

class Foo<E> {
    Cow<E>[] cows = new Cow<E>[4]; // won't work, can't create generic array
}

I don't want to use a list or a set since I want there to always be 4 Cow objects. What's the best solution for me?

Someone
  • 531
  • 3
  • 12
  • 1
    You can't have generic arrays. Why don't you want to use a List? – assylias Feb 06 '14 at 12:54
  • 1
    http://stackoverflow.com/questions/529085/how-to-generic-array-creation – dinesh707 Feb 06 '14 at 12:55
  • I don't want to use a list for two reasons. The first is that you can add and remove elements to and from a list. The second is that I'm creating a quadtree data structure and using a list wouldn't be too good for performance. Quadtrees have a lot of quadrants and using lists would decrease performance significantly. – Someone Feb 06 '14 at 13:00
  • that's a lot of assumptions you have there... – UmNyobe Feb 06 '14 at 13:17
  • implements iterable and a iterator inside your class. see : http://stackoverflow.com/a/4533698/1122645 – UmNyobe Feb 06 '14 at 13:22

5 Answers5

1

If you want to preserve the genericity, you will have to reimplement something similar to a list and I don't think it is worth it.

You said:

The first is that you can add and remove elements to and from a list.

Well you can create an unmodifiable list:

List<E> list = Collections.unmodifiableList(Arrays.asList(a, b, c, d));

The second is that I'm creating a quadtree data structure and using a list wouldn't be too good for performance. Quadtrees have a lot of quadrants and using lists would decrease performance significantly.

First you can initialise the list to the right size:

List<E> list = new ArrayList<>(4);

Once you have done that, the list will only use a little bit more memory than an array (probably 8 bytes: 4 byte for the backing array reference and another 4 byte for the size).

And in terms of performance an ArrayList performs almost as good as an array.

Bottom line: I would start by using a list and measure the performance. If it is not good enough AND it is due to using a list instead of an array, then you will have to adapt your design - but I doubt that this will be your main issue.

Community
  • 1
  • 1
assylias
  • 297,541
  • 71
  • 621
  • 741
0

Use a generic ArrayList and simply have methods to insert values into your object, and do checks inside those methods, to make sure you don't end up having more than 4 Cow objects.

Martin Dinov
  • 8,190
  • 2
  • 26
  • 37
0

Use Collection instead of array:

List<Cow<E>> cows = new ArrayList<>(); // in Java 7

Or

List<Cow<E>> cows = new ArrayList<Cow<E>>(); //Java 6 and below

More information will show why it is IMPOSSIBLE to have arrays whit generics. You can see here

Community
  • 1
  • 1
Ashot Karakhanyan
  • 2,734
  • 3
  • 21
  • 28
0

I will suggest creating a bounded list. Java does not have an inbuilt one however you can create a custom one using Google collections or use the one in Apache collections. See Is there a bounded non-blocking Collection in Java?

Community
  • 1
  • 1
Saket
  • 2,671
  • 3
  • 25
  • 44
0
Cow<E>[] cows = (Cow<E>[])new Cow[4];

or

Cow<E>[] cows = (Cow<E>[])new Cow<?>[4];
newacct
  • 110,405
  • 27
  • 152
  • 217