Home arrow static arrow Java Programming [Archive] - array length
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - array length
This topic has 8 replies on 1 page.

Posts:35
Registered: 7/16/04
array length   
Aug 2, 2004 5:24 AM



 
I define an array and assign value to it , and I want to appent it , how can I do it with all element remained
there unchanged ?

such as , and I want to the 3rd element to it !
int intArr [] = {1,2} ;


 

Posts:21,718
Registered: 98-02-20
Re: array length   
Aug 2, 2004 5:32 AM (reply 1 of 8)



 
You can't append to an array like that.

You either have to use a java.util.List, which is designed to do what you're asking, or reallocate the memory for the array:

int intArr [] = {1,2} ;// Here's now to add a 3rd element - gotta reallocate the memory to include another valueintArr = new int[3];intArr[2] = 3;


%
 

Posts:5,904
Registered: 04/03/99
Re: array length   
Aug 2, 2004 5:33 AM (reply 2 of 8)



 

You can't.

You have to create a new array of the appropriate size and copy the elements into it.

There's a method in the System class called arraycopy which makes this a little easier, but yes, it's basically pretty tedious.

If you are prepared to use Integer objects instead of int primitives, you could use a collection class instead. For example, [url http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html]ArrayList[/url] uses arrays internally, but does all the tedious copying for you whenever it runs out of space.

If you're not familiar with the [url http://java.sun.com/docs/books/tutorial/collections/]Collection classes[/url], now would be an excellent time to learn.

Dave.
 

Posts:743
Registered: 7/12/01
Re: array length   
Aug 2, 2004 5:38 AM (reply 3 of 8)



 
The simple answer is, that arrays are not meant for that.
Use java.util.ArrayList for that.

import java.util.*; public class Test {     public static void main(String[] args) {        ArrayList a = new ArrayList();        a.add(new Integer(1));        a.add(new Integer(2));        for (int ix = 0; ix < a.size(); ++ix) {            int val = ((Integer)a.get(ix)).intValue();            System.out.println("a." + ix + " = " + val);        }    } }

(There is however support in the form of System.arrayCopy.)
 

Posts:21,718
Registered: 98-02-20
Re: array length   
Aug 2, 2004 5:51 AM (reply 4 of 8)



 

You can't.

Oops, I forgot to load the original values into the array. Good thing nobody's going to actually use that mess. ;)

The one problem with all the java.util.Collections is that they don't work with primitives, only objects. (I'm not using 1.5 yet, so I haven't explored the autoboxing feature.) If you really want primitives, you might be advised to write your own collection class that is a resizeable array for using ints.

%
 

Posts:5,965
Registered: 5/17/03
Re: array length   
Aug 2, 2004 5:56 AM (reply 5 of 8)



 
If you really want primitives,
you might be advised to write your own collection
class that is a resizeable array for using ints.

It has already been done,

http://pcj.sourceforge.net/
 

Posts:35
Registered: 7/16/04
Re: array length   
Aug 2, 2004 6:00 AM (reply 6 of 8)



 
Thx very much !
I got it
 

Posts:21,718
Registered: 98-02-20
Re: array length   
Aug 2, 2004 6:04 AM (reply 7 of 8)



 
If you really want primitives,
you might be advised to write your own collection
class that is a resizeable array for using ints.

It has already been done,

http://pcj.sourceforge.net/

Thanks, UJ, I didn't know about this.

%
 

Posts:441
Registered: 2/25/04
Re: array length   
Aug 2, 2004 7:06 AM (reply 8 of 8)



 
There are also similar primitive collections in the apache commons library and in the colt library.

Pete

http://hoschek.home.cern.ch/hoschek/colt/

http://jakarta.apache.org/commons/
 
This topic has 8 replies on 1 page.