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









Related Tutorials/Questions & Answers:
php two dimensional array
php two dimensional array  php two dimensional array example
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
Advertisements
Two Dimensional array program
Two Dimensional array program  consider a two dimensional array... to elements of the array such that the last element become the first one and the first become the last.let the program output elements of the first array
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
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   ... array that contains integer type values. After this, we use two 'for' loop
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
reverse two dimensional arrays in java
reverse two dimensional arrays in java  reverse array elements in two dimensional array such that the last element becomes the first
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
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
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. ... A, and that there are 4 ints in each of those arrays. To process a two-dimensional array, we
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
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
Array in Java
_TO_REPLACE_1 Different types of array used in Java are One-dimensional, Two... = new int[5]; int[] = {50,60,70,80,90};ADS_TO_REPLACE_2 Two-dimensional arrays: Two-dimensional arrays are "an array of arrays". We can have an array of ints
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
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      
Multi-dimensional arrays
that supports it, the element of the two-dimensional array x is denoted by x[i,j]. .... It does, however, supports an array of arrays. In Java, a two-dimensional array...; So far we have studied about the one-dimensional and two
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
two dimansional array - Java Beginners
two dimansional array  Modify the following program so that it computes and prints the tables of powers from 1 to 15.( 1 to 15 to the power 1, Squared, and Cubed: like below) it should look like 1 1 1 2 4 8 3 9 27 and so
Compare two char array in java
Description: This tutorial demonstrate how to compare two character array are equal or not. The Arrays.equals(c1, c2) helps to compare it and return boolean value. Code: import java.util.Arrays
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
Changing the value of Array in Java
;    This is very simple of one dimensional array program. In this tutorial you will learn how to change array values. The one dimensional... dimensional array program. In  this program firstly we are going to define
array - Java Beginners
array  Accept a two dimensional array from the user check if this array is symetric display a message yes,if it is symetric otherwise display it ,is not symetric
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
The Array Palindrome Number in Java
. In this section you will read how to uses palindrome one dimensional array program. The array palindrome number arrange the array number. This session... The Array Palindrome Number in Java   
PHP Push MultiDimensional Array
a multi-dimensional array into another array, a single element into an array, and so on. These all process are done by array_push() function. array_push...-Push.html ADS_TO_REPLACE_3 PHP Push Multi-Dimensional Array Example : <?php
Multiplication of two Matrix
to declare two multidimensional array of type integer. Here in this program use two.... The Java two dimensional array  program is operate to the two matrix number... Multiplication of Two Matrix   
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   
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

Ads