# Two- Dimensional Array

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 February 22, 2010 at 12:48 PM

Hi Friend,

Try the following code:

class BinaryNumbers
{
static int ROWS = 20;
static int COLS = 2;
public static void main(String[] args){
int a2[][]={{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}};
System.out.println("Decimal Binary");
for (int i =0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.print(" " + a2[i][j]);
}
System.out.println("");
}
}
}

Thanks February 23, 2010 at 12:00 AM

Hey,
Here is the code which builds Binary Numbers dynamically where you need not give binary numbers explicitly. Just the decimal no. is sufficient.

public class BinaryNumbers {

public static void main(String[] args) {
int ROWS = 26;
int COLS = 2;
int[][] binaryArray = new int[ROWS][COLS];
for (int i = 1; i < ROWS; i++) {
String binaryNo = Integer.toBinaryString(i);
for (int j = 0; j < COLS - 1; j++) {
binaryArray[i][j] = i;
binaryArray[i][++j] = Integer.parseInt(binaryNo);
}
}
for (int i = 1; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
System.out.print(binaryArray[i][j] + " ");
}
System.out.println("");
}
}

} February 23, 2010 at 12:17 AM

Hi,
Here is another code same as above that prints binary numbers on the fly with little modification.
Just give how many no.of binary numbers you wanna print starting from 1 in the console.May seem complex, but easy..

import java.util.Scanner;

public class BinaryNumbers {

public static void main(String[] args) {
System.out
.println("Enter the no. of binary numbers you want to print : ");
Scanner scan = new Scanner(System.in);
int ROWS = scan.nextInt() + 1;
int[][] binary = new int[ROWS][];
for (int i = 1; i < ROWS; i++) {
String binaryNo = Integer.toBinaryString(i);
binary[i] = new int;
binary[i] = i;
binary[i] = Integer.parseInt(binaryNo);
}
/* Print Array */
for (int i = 1; i < ROWS; i++) {
System.out.println(binary[i] + " " + binary[i]);
}
}
}

Snippet of o/p:

Enter the no. of binary numbers you want to print :
40
1 1
2 10
3 11
4 100
.
.
.
38 100110
39 100111
40 101000

Regards,
javaquest2010 February 26, 2010 at 7:32 AM

Thank you very much every one

two dimensional array
two dimensional array  how tow dimensional array works.How those loopes get incremented .and how every time the value of k changes
Two Dimensional Array Program
Two Dimensional Array Program Using Nested for loop...; program . In this session we will teach how to use of a two dimensional array... are going to make two dimensional array having three row and three columns.
Two- Dimensional Array - Java Beginners
Two- Dimensional Array  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
how do i begin a two dimensional array?
how do i begin a two dimensional array?  I'm new to java programming and need to create a two dimensional array that enters exactly what is entered in the first dimension and then the first non-white space character of what
Two dimensional array in java
Two dimensional array in java. In this section you will learn about two-dimensional array in java with an example. As we know that array is a collection.... int arr[][]; Here arr is a two-dimensional array of type int. To create an array
A single dimensional array is called
A single dimensional array is called  What is a single dimensional array called?   A single dimensional array is called LIST
need help with two dimensional array that takes input from user and bubble sorts and selections sorts
need help with two dimensional array that takes input from user and bubble... cannot figure out how i can declare a two dimensional array without knowing how... many words he/she types without using an array list.   Thank you. I
two dimensional - Java Beginners
two dimensional  write a program to create a 3*3 array and print the sum of all the numbers stored in it.  Hi Friend, Try the following code: import java.io.*; import java.util.*; public class matrix
one dimensional array program
one dimensional array program  Design and implement a java program that will read a file containing numbers and compute the following statistics: the rannge( low, high), the average and the median(middle number). Notes
one dimensional array using java
one dimensional array using java  design and implement a java program that will read a file containing numbers and compute the following statistics: the range(low,high) the average and the median
Array in Java
. Different types of array used in Java are One-dimensional, Two-dimensional and multi...[] = {50,60,70,80,90}; Two-dimensional arrays: Two-dimensional arrays...An Array is the static memory allocation that holds a fixed number of values
Need coding help - two dimensional matrix and it returns back a boolean.
Need coding help - two dimensional matrix and it returns back a boolean.  I need to write a code method that accepts a two dimensional integer arrays as the argument and returns true if the array or matrix is left or right
Display tow dimensional array using one for loop in java
Display tow dimensional array using one for loop in java  Display tow dimensional array using one for loop in java
Display tow dimensional array by matrix form using one for loop in java
Display tow dimensional array by matrix form using one for loop in java  Display tow dimensional array by matrix form using one for loop in java
C Array of String
and handled like a 2d(two dimensional) arrays. You can see in the given example that we have declare a 2 dimensional character array consisting of three 'rows' and twelve... C Array of String
plz try to clear my doubt for shuffling multi-dimensional array
plz try to clear my doubt for shuffling multi-dimensional array  hi... want to shuffle the ful entire multi-simensional array means wat v want to do... final int size = 5; private int[][] array = new int[size][size]; private
Creation Time Comparison of Multi Dimensional Array- Java Tutorials
Creation Time Comparison of Multi Dimensional Array In this section, we will compare the creation time between the different size/dimension of array.  As you know that multidimensional array is the array of arrays. Practically
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
How to read text file to two different name array
How to read text file to two different name array   I have those numbers:12,4,9,5 numbers:19,12,1,1 how to put it in two different name array in text file to java
how to store array values into two different tables in java?
how to store array values into two different tables in java?  I have use 4/5 textboxes with same name(e.g.text1) and I get the values in a array and now I want to store these values in two different tables(i.e store 2 array
Matrix multiplication in java
multiplication of two matrices. In java this is a simple program to multiply two matrices, we have to take two-dimensional array and the result should be saved in third two-dimensional array. Here we are going to develop a Java code for matrices
Array Strings
Array Strings  String auto[]=new String[]{"Length: ","Breadth: ","Height: "}; in the above code i can easily input values into my string auto which is a single dimensional array. How do you do this with a two dimensional array
Array
reserved. Use a one-dimensional array of primitive type Boolean to represent the seating chart of the cinema theater. Initialize all the elements of the array... the corresponding elements of the array to true to indicate that the seat is no longer
in_array
in_array  in_array in php
is _array()
is _array()  is_array() in php   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php \$yes = array('Hello', 'World'); echo is_array(\$yes) ? 'Array
is _array()
is _array()  is _array()   Hi Friend, This function is of Boolean type.It checks whether a variable is an array or not. Here is an example: <?php \$yes = array('Hello', 'World'); echo is_array(\$yes) ? 'Array
Array
Array  how can i use elements of an array in a circular form? that is if the searching of the element reach the last elements of the array, then it will start serching from the begining of the array
Array
Array  is it possible to define array like this..? int[] intArray = new int[] {4,5,6,7,8}; Explain...?   Yes, you can. Java Initialize Array