Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-08-2007, 11:14 AM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
creating array at runtime
Hi,

I want to create an array at run time. I am not sure about its size when coding. I take an input from user through console and then want to create the array of that size.

Code:
int[] array = new int [5];
In the example above, I mentioned the size which is fine. I just want to know if size can be given later?

Is this possible? How?

Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-08-2007, 11:37 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
if size can be given later
No. You have to instantiate an array with a size. You can change the size later by making a new array of the same type at a different size and copying the elements of the old array into it - actually assigning the new elements the value of the old elements. The System.arraycopy method is useful for this.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-08-2007, 02:54 PM
Member
 
Join Date: Jun 2007
Location: Colombo, Sri Lanka
Posts: 31
hiranya is on a distinguished road
You could do something like this.

Code:
int[] array = new int[size];
Here size is an integer variable with some value. If the value of 'size' is determined at the runtime then your array size will be determined at runtime.

Regards,
Hiranya
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-08-2007, 11:13 PM
Member
 
Join Date: Nov 2007
Posts: 97
javaplus is on a distinguished road
Thanks. Its helps.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-09-2007, 12:06 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
I just want to know if size can be given later?
You can re/instantiate the array at any time.
Code:
class Pseudo { int[] sizes; // declaration String[] ids = new String[0]; // declaration and instantiation; void someMethod() { // Instantiate the sizes array declared as a member variable. // Java assigns each element the default value of zero. sizes = new int[16]; // Instantiate the ids array declared and insantiated as a // (zero-length array) member variable. // Each element of this array is null. ids = new String[4]; } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an array of nonprimitive objects Java Tip java.lang 0 04-14-2008 10:46 PM
Need help with creating array of type object riz618 New To Java 3 01-29-2008 08:14 AM
Creating Array of LinkedList sasikumardr New To Java 1 12-11-2007 12:25 PM
How to initialize array at runtime Java Tip Java Tips 0 11-09-2007 05:47 PM
Initialize array at runtime javaplus Java Tips 2 11-09-2007 01:44 PM


All times are GMT +3. The time now is 09:14 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org