-5

I want to have a array of integers where the length is variable. The obvious choice is to use ArrayList but can I do this for primitive types such as

ArrayList<int> myArray=new ArrayList<int>();

I dont want to use

ArrayList<Integer> 

because the Integer class is clumsy in terms of coding.

EDIT: From the answers below I think the solution is to write my own Integer class.

To answer the question below about "clumsy" let me give a specific, and I would of thought common use for integers namely using the last member of the array in any place you would want the integer. If I just call the array "name" then to get the actual integer that can be operated on I need

name.get(name.size()-1).intValue();

To me this seems like an awfully unwieldy expression for a simple integer - particularly if it appears in an expression twice. It also seems that (most of the) methods available for the Integer class are absolutely redundant. Take two examples

static int compare(int a, int b)

Quite unbelievably, according to the documentation, this method returns a-b!!

static Integer valueOf(int a)

returns an Integer instance of the integer a. Can someone give me a single example where

new Integer(a)

does not achieve exactly the same result?

user3799584
  • 917
  • 1
  • 9
  • 18

3 Answers3

2

Method 1: (not recommended)

You can do something like this, but this doubles the code and is not efficient:

int[] a;

//get size (from command line maybe ow whatever method you want)

You can set size 0 initially, and for ex. you are transferring values from arraylist so you will have to write:

 while(itr.hasNext()){
  size++;} //itr is an object of Iterator
 int i=0;
 a=new int[size];
 // then loop again to store values
 while(itr.hasNext()){
  a[i]=itr.next();
  i++;} 

Method 2:

Or you may use ArrayList without making it clumsy as follows:

ArrayList al=new ArrayList();

then you may declare Integer objects as volatile and perform operations on them just as you do with the primitive types.

Method 3: (not recommended)

Or simply write:

ArrayList al=new ArrayList();//ignore the warning about <E>
int x=2;
al.add(2);

Method 4: (recommended)

If I were you I would use ArrayList<Integer>.

UPDATE: Another thing that might work is that you may initially create an ArrayList<Integer> and store values there and later convert it to int[]

This SO answer tells about the conversion. Quoted the code form there:

public static int[] convertIntegers(List<Integer> integers)
  {
     int[] ret = new int[integers.size()];
      for (int i=0; i < ret.length; i++)
      {
         ret[i] = integers.get(i).intValue();
      }
     return ret;
   }

Hope this helps.

Community
  • 1
  • 1
user63762453
  • 1,734
  • 2
  • 22
  • 44
1

No it's not possible to use primitive types as generic type.

ydemartino
  • 242
  • 5
  • 10
0

Well I would recommend you do use ArrayList and avoid primitive types in this case.

You can't change the size of an array once created. You have to allocate it bigger than you think you'll ever need

or

Accept the overhead of having to reallocate it to a new larger array and copy the data from the old to the new:

System.arraycopy(oldItems, 0, newItems, 0, 10);

But Much simpler to go with ArrayList.

Michael W
  • 3,515
  • 8
  • 39
  • 62