File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Wanted some help about arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wanted some help about arrays" Watch "Wanted some help about arrays" New topic
Author

Wanted some help about arrays

swapnil deo
Greenhorn

Joined: May 30, 2007
Posts: 6
WELL IM JUST A BEGENINER FOR JAVA...AND IM GETTING SOME PROBLEMS WHILE INITIALIZING A MULTI DIMENSIONAL ARRAY..!!!

ACTUALL WHAT I WANTED TO DO IS..DECLARE A MULTI DIMENSIONAL ARRAY...AND WANT TO ENTER THE ELEMENTS IN RUNTIME....I.E. I WANT THE VALUES TO BE ENTERD BY THE USER...

IF ANY1 CAN PLZZ HELP ME OUT WID SOLVING DIS PROBLEM...
WAITING FOR YOUR REPLIES PEOPLE...PLZZ HELP ME


EDIT by mw: Removed shouting from subject line.
[ May 30, 2007: Message edited by: marc weber ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36453
    
  15
Welcome to the Ranch.
Please don't use upper-case throughout and please don't use abbreviations like ANY1.

Have you been through the Java Tutorial about arrays? You realise there is, strictly speaking, no such thing as a multi-dimensional array in Java, only arrays of arrays. The examples in the link I gave you tell you how to set up arrays; you would have to ask how many entries there are going to be.

Example: "Mr Campbell Ritchie" can be divided into a 3-member String[] using the split() method of the String class and " " as its regular expression. Similarly "Swapnil Deo" can be divided into a 2-member String[].
You can use the length parameter of that array to set up a new array, and copy all the members in. Or even easier, set up a String[][] array, and copy your arrays into its members.

Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Welcome swapnil,

If you have a "one-dimensional" array, the natural control structure to implement elements is one loop.

If you have a "two-dimensional" array (really an array of arrays), the natural control stucture to initialize elements is a doubly-nested loop.

etc.

In addition to arrays (which are part of the Java language), see also the classs: ArrayList etc.

Kaydell
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Can you show us some code to see how far you've gotten and where you are stuck? If the whole thigns sounds overwhelming, maybe just do a one-dimensional array and see if you're getting values from the user and putting them into the array ok.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

One clue is that in Java, array indexes start with a zero index and go to length minus one.

Kaydell
swapnil deo
Greenhorn

Joined: May 30, 2007
Posts: 6
thanks everyone for your replies...but still im not clear with this concept of "array of arrays"

Campbell Ritchie : i went through the link which you gave and read things about array but the program over there was as follows : class ArrayDemo {
public static void main(String[] args) {
int[] anArray; // declares an array of integers

anArray = new int[10]; // allocates memory for 10 integers

anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.
anArray[3] = 400;
anArray[4] = 500;
anArray[5] = 600;
anArray[6] = 700;
anArray[7] = 800;
anArray[8] = 900;
anArray[9] = 1000;

System.out.println("Element at index 0: " + anArray[0]);
System.out.println("Element at index 1: " + anArray[1]);
System.out.println("Element at index 2: " + anArray[2]);
System.out.println("Element at index 3: " + anArray[3]);
System.out.println("Element at index 4: " + anArray[4]);
System.out.println("Element at index 5: " + anArray[5]);
System.out.println("Element at index 6: " + anArray[6]);
System.out.println("Element at index 7: " + anArray[7]);
System.out.println("Element at index 8: " + anArray[8]);
System.out.println("Element at index 9: " + anArray[9]);
}
}

The output from this program is:

Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000

im pretty clear with the above code
now in the above code we are initializing the array at the very begening..ie in the code itself..but my doubt was how do we insert values in runtime like in the following C++ code :
int a[][];
int x;
cout<< "enter dim of array";
cin>> x;
cout << "enter array elements";

for(int i=0;i<x;i++)//this for loop will ask user to enter values at runtime
{
for (int j=0;j<x;j++)
{
cin>>a[i][j]; //user will enter values when this line gets executed
}
}
for(int i=0;i<x;i++)//this for loop will print the output
{
for (int j=0;j<x;j++)
{
cout<<"print";
cout<<a[i][j];
}
}

o/p:
enter dim of array:
3 3
enter array elements:
1 2 3 4 5 6

print: 1 2 3 4 5 6



now in this way...how do i get to enter values to 2-dimensional arrays in java..???
this is my real doubt...???
thanks for your time..!!
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Note that cin and cout are C++ streams. In Java the standard streams are:

System.in for standard input
System.out for standard output
and
System.err for error output.

I found something useful at the following website:

Example of using System.in



The above example shows you how to read from the standard input and write to the standard output, but leaves you some work so that you can learn by writing your own code.

One more hint is that to create an array, you can use the following syntax:

int[][] intArray = new int[5][6];

The line of code above will create a 5 by 6 array of int. The array sizes don't have to be known at compile-time, so you can read the array sizes from the console and use x & y for the array sizes.

Kaydell
[ June 03, 2007: Message edited by: Kaydell Leavitt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wanted some help about arrays
 
Similar Threads
multidimensional arrrays
2 dim array
tutorial of 2-dimensional arrays
Speech to text conversion using JAVA
about hashmap implementation of arraylist