This week's book giveaway is in the Android forum.
We're giving away four copies of Head First Android and have Dawn & David Griffiths on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to initialize array dynamically? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to initialize array dynamically?" Watch "How to initialize array dynamically?" New topic

How to initialize array dynamically?

Amruta Joshi

Joined: Jul 08, 2005
Posts: 1
How to initializa array at run time without using for loop.?
I there any other method for that?
Mani Ram
Ranch Hand

Joined: Mar 11, 2002
Posts: 1140
With out a loop!?

One Way:

Another Way:
You can use the fill() method in java.util.Arrays class, (but it internally uses a loop to assign the values).

Yet Another Way:
You can use while loops, if you don't want to use for loop

[ July 08, 2005: Message edited by: Mani Ram ]

Quaerendo Invenietis
Amol Fuke
Ranch Hand

Joined: Apr 08, 2005
Posts: 129

You can use something like this:

double[][] xAxisValues = new double[xValuesList.size()][];

This will initialize array xAxisValues of size xValuesList.size() where xValuesList is arraylist.

"There are no mistakes, only lessons"
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
I did it my way (c):

[ July 08, 2005: Message edited by: George Bolyuba ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
I agree. Here's the link:
subject: How to initialize array dynamically?
It's not a secret anymore!