• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a Java ArrayList of one of either types, but not both  RSS feed

 
Jeremy Watts
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would I create a Java ArrayList, that will allow one of two types to be added to it, but not both? So, if the two types were Integer & String, then I would be able to add instances of either Integer or String, but not a mixture of the two?

Jeremy
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean something like this?

List<String> strList = new ArrayList<String>()

or

List<Integer> intList = new ArrayList<Integer>()
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you confuse yourself? If you do not want to have your ArrayList to contain a mixture of Integer and String, why do you need one could take both an Integer and a String? Since Java 5, with generics, you could only put one type inside a collection. If you want to defeat that, you have to then provide your own implementation of the ArrayList that could take a type which is either a String or an Integer!
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can't be done I'm afraid, at least not by using generics. That is if I understand you correctly. Do you mean to create an ArrayList to which at runtime either String or Integer elements can be added, but once the first element of a type has been added, the ArrayList will be bound to that type exclusively? If that is the case I'm struggling to come up with a use for such a requirement, maybe you could elaborate?
 
Jeremy Watts
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes sorry, should have been more specific. What I want is for, during runtime, for either Integers, or Strings, only to be 'addable' to the list - but if an attempt were made to add a String to an already created List of Integers, then an error be thrown, and vice versa.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want something that IS-A ArrayList to offer you that functionality, you'll have to create your own subclass of ArrayList and override the methods that can add elements to it to perform type checking. Be aware that ArrayList references the contract established by Collection which states:

...
If a collection refuses to add a particular element for any reason other than that it already contains the element, it must throw an exception (rather than returning false).
...
@throws ClassCastException if the class of the specified element prevents it from being added to this collection
...

If you want/need to deviate from that, you should clearly document it.

I still don't see why you'd want or need something like this, though. Could you elaborate some more about how you intend to use such an implementation?
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Jelle that it is hard to see a use for such a List, but you could write a wrapper class around an ordinary ArrayList and check types. Maybe you would use getClass() on every element added.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy Watts wrote:Yes sorry, should have been more specific. What I want is for, during runtime, for either Integers, or Strings, only to be 'addable' to the list - but if an attempt were made to add a String to an already created List of Integers, then an error be thrown, and vice versa.


But isn't that what you already get at compile time when you declare an ArrayList of type String or Integer?

 
Jeremy Watts
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I think what I originally put in the question, isn't a very good analogy to what I'm trying to do, which is this:-

I have an ArrayList, defined as:-

ArrayList<Item> factors = new ArrayList<Item>();

Where, 'Item' is an inner class, defined as :-

public static class Item implements Comparable<Item>
{
private int factorPower;
private UnivariatePolynomial factor;

public Item(UnivariatePolynomial factor, int factorPower)
{
this.factor = factor;
this.factorPower = factorPower;
}
...........
}


And here, 'UnivariatePolynomial' is an abstract class that has two classes that extend it. These are:-
'UPOverFiniteField', and 'UPOverZ'.

So, what I'm trying to do is to allow a list of one 'type' of item only, but to throw an error if one type is added to a list of the other.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Type safety is checked at compile time. For your List, you don't know until runtime, until when the first element is added to the list, which kind of elements should be allowed. So, you cannot do this in a way that is type safe, because you don't know at compile time what the type of elements is that are allowed in the list. There's no way you can make it so that the compiler will give you an error.

So, the only way that remains is checking it at runtime - do a check whenever an element is added to the list, and if it isn't the expected type, throw an exception.

*edit* - Here's an idea; you could make class Item generic. Like this:

Then you can create a list like this:
 
Jeremy Watts
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many Thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!