• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wanted some help about arrays

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Kaydell Leavitt
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One clue is that in Java, array indexes start with a zero index and go to length minus one.

Kaydell
 
swapnil deo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
A teeny tiny vulgar attempt to get you to buy our stuff
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic