Hello,
I have a little problem with integer arrays ...
I need int[] arrays in some functions.
But when I create these int[], I don't know the int[] size at the
beginning because I add data in a loop. I tried to use ArrayList :
ArrayList<Integer> arr = new ArrayList<Integer>();
while(...) {
int n = ...;
arr.add(n);
}
But at the end I can't get the result in a int[] array ! I only get
Object[] array that I can't convert to int[].
Do you know what's the best way to use int[] buffers ?
Thank you,
Olivier

Signature
Olivier Ligny
www.virgal.net (Monde persistant)
Ian Wilson - 17 Jul 2007 16:25 GMT
> Hello,
>
[quoted text clipped - 9 lines]
> But at the end I can't get the result in a int[] array ! I only get
> Object[] array that I can't convert to int[].
import java.util.ArrayList;
public class IntArrayList {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(1);
list.add(5);
int[] ints = new int[list.size()];
for (int i = 0; i < ints.length; i++)
ints[i] = list.get(i);
}
}
It is marginally easier if your functions can use Integer[].
Hendrik Maryns - 17 Jul 2007 16:26 GMT
O.L. schreef:
> Hello,
>
[quoted text clipped - 11 lines]
>
> Do you know what's the best way to use int[] buffers ?
Use ArrayList. Why do you need the int[]? What can you do with it that
you cannot do with ArrayList?
If you really want the int, you can do myList.toArray(new
Integer[myList.size()])
This gives you an Integer[]. Autoboxing will do the rest.
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Ian Wilson - 17 Jul 2007 16:58 GMT
> O.L. schreef:
>
[quoted text clipped - 7 lines]
>
> This gives you an Integer[]. Autoboxing will do the rest.
I suspect autoboxing won't convert object arrays to native arrays.
Eclipse gives me a compiler error: "The method emit(int[]) in the type
IntArrayList is not applicable for the arguments (Integer[])"
-------------------------------------8<----------------------------
import java.util.ArrayList;
public class IntArrayList {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(2);
list.add(1);
list.add(5);
// 1. Use Integer[]
Integer[] intArray = list.toArray(new Integer[0]);
emit(intArray); // ******** compiler error ********
// 2. use int[]
int[] ints = new int[list.size()];
for (int i = 0; i < ints.length; i++)
ints[i] = list.get(i);
emit(ints);
}
static void emit(int[] intz) {
for (int i = 0; i< intz.length; i++)
System.out.println(intz[i]);
}
}
-------------------------------------8<----------------------------
Am I missing something?
Roedy Green - 17 Jul 2007 19:57 GMT
On Tue, 17 Jul 2007 16:58:10 +0100, Ian Wilson
<scobloke2@infotop.co.uk> wrote, quoted or indirectly quoted someone
who said :
>I suspect autoboxing won't convert object arrays to native arrays.
However, if you use <Integer> you can fill your array with Integers
and they will automatically cast back out as Integer. Autoboxing will
then convert Integer to int for you on a field by field basis.
You can't convert an Integer[] to int[] with autoboxing. You have to
do that with a field by field copy.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Eric Sosman - 17 Jul 2007 17:17 GMT
O.L. wrote On 07/17/07 10:43,:
> Hello,
>
[quoted text clipped - 9 lines]
> But at the end I can't get the result in a int[] array ! I only get
> Object[] array that I can't convert to int[].
The list holds Integer objects, not int values. You
could get an Integer[] from the list (there's more than
one method named toArray), but there's no way to "convert"
that array of object references to an array of int values.
You will need to extract the values, one by one.
> Do you know what's the best way to use int[] buffers ?
What's the best way to define "best?"
One possibility is to proceed as you've begun, and
to follow the loop with
int[] array = new int[ arr.size() ];
for (int i = 0; i < array.length; ++i)
array[i] = arr.get(i).intValue();
Another is to guess at a likely array size and start
filling it, replacing it with larger arrays if needed and
perhaps with a truncated array at the end:
int[] array = new int[42];
int count = 0;
while (...) {
int n = ...;
if (count == array.length) {
int[] temp = new int[ count * 2 ];
System.arrayCopy(array, 0, temp, 0, count);
array = temp;
}
array[count++] = n;
}
// optional:
if (count < array.length) {
int[] temp = new int[count];
System.arrayCopy(array, 0, temp, 0, count);
array = temp;
}
Still another is to ponder how badly you need an array
rather than (for example) the List you've already built.

Signature
Eric.Sosman@sun.com
O.L. - 17 Jul 2007 19:42 GMT
Eric Sosman a formulé la demande :
> [...]
> Another is to guess at a likely array size and start
[quoted text clipped - 18 lines]
> array = temp;
> }
Hello,
I followed your suggestion : I used an hybrid system with a custom
IntArray class, without using the ArrayList class, so I have a great
freedom of movement.
Here is the source : http://tinyurl.com/3bwy38
Datas are stored in a private int[] array, but all the useful public
methods are implemented (.contains(int), .add(int) ...). So I don't use
int[] array but a class which wraps an int[] array, like ArrayList I
suppose (?).
I did benchmark tests to compare ArrayList and IntArray/int[], and it's
the same.
Thank you very much for all your answers, and excuse my bad english ;)
Olivier

Signature
Olivier Ligny
www.virgal.net (Monde persistant)
Lew - 17 Jul 2007 21:51 GMT
> I followed your suggestion : I used an hybrid system with a custom
> IntArray class, without using the ArrayList class, so I have a great
[quoted text clipped - 9 lines]
>
> Thank you very much for all your answers, and excuse my bad english ;)
Congratulations, you've just re-invented ArrayList, only without generics.

Signature
Lew
Eric Sosman - 17 Jul 2007 22:49 GMT
Lew wrote On 07/17/07 16:51,:
>>I followed your suggestion : I used an hybrid system with a custom
>>IntArray class, without using the ArrayList class, so I have a great
[quoted text clipped - 11 lines]
>
> Congratulations, you've just re-invented ArrayList, only without generics.
... and with primitives instead of references. If the
arrays are largish, expansion by a factor of five-ish
might be worth avoiding.

Signature
Eric.Sosman@sun.com
Lew - 18 Jul 2007 00:25 GMT
Lew wrote:
>> Congratulations, you've just re-invented ArrayList, only without generics.
> ... and with primitives instead of references. If the
> arrays are largish, expansion by a factor of five-ish
> might be worth avoiding.
Good point.

Signature
Lew
Zig - 18 Jul 2007 05:12 GMT
Probably a little late, but the OP may want to take a look at the Apache
Primitives Project:
http://jakarta.apache.org/commons/primitives/
This looks a lot like
org.apache.commons.collections.primitives.ArrayIntList (plus the union
method)
http://jakarta.apache.org/commons/primitives/api-release/org/apache/commons/coll
ections/primitives/ArrayIntList.html
>> I followed your suggestion : I used an hybrid system with a custom
>> IntArray class, without using the ArrayList class, so I have a great
[quoted text clipped - 10 lines]
> Congratulations, you've just re-invented ArrayList, only without
> generics.
O.L. - 18 Jul 2007 09:11 GMT
Lew a exprimé avec précision :
>> I followed your suggestion : I used an hybrid system with a custom IntArray
>> class, without using the ArrayList class, so I have a great freedom of
[quoted text clipped - 11 lines]
>
> Congratulations, you've just re-invented ArrayList, only without generics.
I loooove to reinvent the wheel ;)

Signature
Olivier Ligny
www.virgal.net (Monde persistant)
Roedy Green - 17 Jul 2007 19:55 GMT
>But at the end I can't get the result in a int[] array ! I only get
>Object[] array that I can't convert to int[].
see http://mindprod.com/jgloss/array.html
You can get an Integer[] out of the ArrayList. You then have to
convert this to an int[] by allocating a new int[] and copying field
by field.

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Daniel Pitts - 18 Jul 2007 18:40 GMT
> Hello,
>
[quoted text clipped - 17 lines]
> --
> Olivier Lignywww.virgal.net(Monde persistant)
Either you have to know before hand the size of the array, or use an
List<Integer> list, and then create a int[] arr = new
int[list.size()];
Then copy all of the values.
In general, unless you have an overwhelming need to use an array,
you're better off using the less primitive construct, such as a List
or Collection.