Two- Dimensional Array

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
how can i do this? Please help me
View Answers

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[2];
binary[i][0] = i;
binary[i][1] = Integer.parseInt(binaryNo);
}
/* Print Array */
for (int i = 1; i < ROWS; i++) {
System.out.println(binary[i][0] + " " + binary[i][1]);
}
}
}

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

Ads









Related Tutorials/Questions & Answers:
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. 
Advertisements
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
Two Dimensional Array Program
Two Dimensional Array Program    ... will learn how to display arrange form of two dimensional array program... a integer for array declaration Two dimensional array program. We are going
Square Elements of Two Dimensional Array
the two dimensional array program and its square. This session provide you... Square Elements of Two Dimensional Array   ...: We are going to display the square of two matrix. Firstly, we have to define
Two Dimensional Array Program Using Nested For Loop
Two Dimensional Array Program Using Nested For Loop ... dimensional array program. Firstly, we have to define class "TwoDimensional ". We are going to make a integer for array declaration Two dimensional 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
Dividing Element of Two Dimensional Array
Dividing  Element of Two Dimensional Array...;ArrayDivideMatrix". Here you will learn how to use two matrix array for developing Java program.  The java two dimensional array program is operate the two
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
Declare string array in Java (one and two dimensional)
Declare string array in Java (one and two dimensional... dimensional array and two dimensional as well. 1. String arr[] = new String...] + " "); } //declare and initialize two dimensional array String dim2
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
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
Two-dimensional arrays
; Two-dimensional arrays are defined as "an array of arrays"... of arrays of  ints". Such an array is said to be a two-dimensional array. ... in each of those arrays. To process a two-dimensional array, we use nested
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
Three Dimensional Array program
Three Dimensional Array program   ... will learn how to use three dimensional array. Firstly, we have to define class... are going to make three dimensional array having multi rows and columns. By using
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      
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
Multidimensional Array Java
to store it in an array. We use mostly two types of arrays that is simple array and multi dimensional array.   In the example given below we have used two dimensional array. A two dimensional array can be thought as a grid
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
Dividing of two Matrix in Java
;Here you will learn how to use two matrix array for developing Java program.  The java two dimensional array program is operate the two matrix. Now we... Dividing of two Matrix in Java   
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
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
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
How to declare String array in Java?
elements of one dimensional array and two dimensional array. 1. String arr... and initialize two dimensional array String dim2... Nishant Shashank Gitika These are elements of two Dim array. Somendra
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats... the array {10.1,11.2,8.3,7.5,22} into{22,7.5,8.3,11.2,10.1
Array
Array  How do i insert elements into an array up to a limit from...("Enter Range: "); int size=input.nextInt(); int array[]=new int[size]; System.out.println("Enter Array Elements: "); for(int i=0;i<
array
array  WAP in java to store 6 element in array P and 4 element in array Q. Produce the third arra y R containing all element from p & q
array
array  array memory allocation is dynamic or static in java   Java Arrays have dynamic memory allocation
array
array  create an array in which no element is duplicate. if duplicate then tell to user duplicate. duplicacy is tell to user during when he enter the element
array
array  create an array in which no element is duplicate. if duplicate then tell to user duplicate. duplicacy is tell to user during when he enter the element
array
array  take a 2d array and display all its elements in a matrix fome using only one for loop and ple explain the program in below

Ads