Question:
Two-dimensional array?
anonymous
2010-02-21 14:33:42 UTC
I am new in java programming. I am creating a two-dimensional array. This is my code
**
class BinaryNumbers
{
public static void main(String[] args)
{
//create a two-dimensional array
int ROWS = 21;
int COLS = 2;

int[][] a2 = new int[ROWS][COLS];

//... Print array in rectangular form
for (int i =0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.print(" " + a2[i][j]);
}
System.out.println("");
}
}
}
I want to enter this data into this array
Decimal Binary
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
16 10000
17 10001
18 10010
19 10011
20 10100
how can i do this? Please help me
Also do give me answers keeping in mind that I am new in java
Four answers:
Tom
2010-02-21 14:42:08 UTC
Like I said before you deleted the question you asked earlier, you need to create another for loop to assign the correct values to each part of the array. Until you do this, your array has nothing but 0's in it. I'm not going to provide you exact code on how to do this because this looks to be a "homework" assignment you are working on and it's best to learn how to do it yourself instead of being given the answer. Most other people will tell you the same thing, we aren't here to do your homework for you. If you are still learning Java, then do a search for Java tutorials on yahoo or google or whatever search engine you want and there will be many out there that will assist.
MichaelInScarborough
2010-02-21 14:45:34 UTC
I suggest you don't need a 2 dimensional array as the numbers are consecutive.

int[] a2 = {

1, 10, 11, 100, 101,

110, 111, 1000, 1001, 1010,

1011, 1100, 1101, 1110, 1111,

10000, 10001, 10010,10011, 10100,

};

In case you insist on a 1 based index, you could leave a 0 infront of the first array element.
anonymous
2016-10-29 00:42:08 UTC
A 2 dimensional array may well be called a grid. each and each sq. in the grid is a container out of your occasion. to hold the container topic extra you could think of of ordering a team of bins in a sq. or oblong shape. To get admission to merchandise 4, 3 you may bypass over 4 columns and down 3 rows and that's the article you opt for. to pass to much greater dimensions a three dimensional array may well be however of a dice decrease into smaller cubes. you progression on each and each get admission to of the dice to locate the index you're searching for for. (notice that visualizing 4 dimensional arrays gets greater complicated.) whilst ever i'm having worry conceptualizing my arrays (for a million-3 dimensions), i detect it useful to entice them on grid paper. that's what i decide for to recommend for you. on the same time as products may well be moved out and in of arrays, the spot maintains to be. you're marvelous in thinking that it is not like a itemizing in this manner. If an merchandise is deleted from an array then that spot is empty (or null). It does not exchange the addresses (places) of the different products. Vaccano
CS_Q
2010-02-21 14:39:39 UTC
I haven't programmed in Java.. but I think the way to enter the data into the array is by using the same for loops you used to print out the content of the array but this time to store a variable into the array...



try to use the same command you use to store an integer..



in c++:

to print out something, we use: cout << x;

to store something in, we use: sin >> x;


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...