Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JAVA - Arrays

Status
Not open for further replies.

munnanext

Programmer
Joined
Aug 20, 2004
Messages
49
Location
US
Hi, I have a Question regarding the Arrays. I am new and trying to understand how Arrays works.

When I am reading some material it Says that I need to create an Array like below

Code with using NEW
===================
Note: Here they used NEW
String[] anArray = new String[5];
for (int i=0; i<5; i++)
{
//Asigned dat to the Array
}

Code with OUT using NEW
=====================

public class myArray{
public static void main(String[] args)
{
String[] anArray = {"String one","String Two","String Three" };
}
}

In the just above exanple in the line
String[] anArray = {"String one","String Two","String

the Array anArray is not creted using NEW Keyword...

Can you pl explain me why sometimes NEW and SOmetimed not ?

Appreciate your Time
 
you would use "new" when you want to initialize the size of an array, but not set its contents

String[] anArray = new String[5];
creates a string array of size 5, but every element is null

String[] anArray = {"String one","String Two","String Three" };
creates an array of size 3, with each element defined.

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
See it as syntax-sugar, only available for Arrays.

Other classes have constructors:
Code:
musician zappa = new Musician ("Frank", "Zappa", "Composition,Lyrics,Vocals");
For Arrays, the number of elements isn't known in general, that might be one of the reasons, for making an exception.
Another one: Every array is of different type (String [], int [], Musician[], ...).

seeking a job as java-programmer in Berlin:
 
Thanks for the reply. Can you pl explain me How to know the Size of the Array ?
I know when the Array is One Dimension then we need to get the Size like

int[] myArray = new int[];
//Size of teh Array
System.out.println(myArray.length)

But if I have Two Dimensional Array like below
int[][] myArray = new int[5][5];

Let's say I am string like
myArray[COLUMNS][ROWS]

How should I know # of columns and #of Rows ?

Thanks for your help.
 
Code:
int[][] myArray = new int[5][4];
int l = myArray.length;
for (int i = 0; i < l; ++i)
{
	int j = myArray[i].length;
	System.out.println ("arr " + i + " len=" + j);
}
Because every row may have a different number of columns, I tested every row here. See this example:
Code:
int[][] myArray = new int[3][];
myArray[0] =  new int [3];
myArray[1] =  new int [6];
myArray[2] =  new int [9];

int l = myArray.length;
for (int i = 0; i < l; ++i)
{
	int j = myArray[i].length;
	System.out.println ("arr " + i + " len=" + j);
}



seeking a job as java-programmer in Berlin:
 
Hi,
Can Any One Explain about threads in Java. I am new to Java....
Appreciate your time
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top