Creating a number gird in Java - java

I'm trying to create a number grid in Java that would look like this:
789
456
132
public class Test {
public static void main(String[] args) {
//create grid
int [] [] grid = new int [3][3];
grid [0][0] = 7;
grid [1][0] = 8;
grid [2][0] = 9;
grid [0][1] = 4;
grid [1][1] = 5;
grid [2][1] = 6;
grid [0][2] = 1;
grid [1][2] = 2;
grid [2][2] = 3;
int rows = 3;
int columns = 3;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println(grid[i][j] + " ");
}
System.out.println("");
}
}
}
the result that I'm getting is:
7
4
1
8
5
2
9
6
3
Any suggestions on to where I am going wrong? Many thanks in advance

System.out.println() prints a newline every time. Instead use System.out.print() in the inner loop. Also you need to switch i and j when printing:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(grid[j][i] + " ");
}
System.out.println("");
}

use System.out.print(grid[j][i] + " "); you need to swap the position ofi and j because your numbers entered are in columns whereas you want them to be printed in rows.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(grid[j][i] + " ");
}
System.out.println("");
}
output
7 8 9
4 5 6
1 2 3

Related

Printing the even numbers in the columns of 2D array

hello guys I am struggling to find the right way to print only the even numbers of the array.
I made a 1 dimensionnal array to save the element of the columns and then make sure if the index of the col%2==0 put that number in the output.
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//array with 3 row in 5 col
int[][] matrix = new int[3][5];
//int []y = new int[5];
// to impalement th array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.print("Enter matrix[" + i + "][" + j + "]: ");
matrix[i][j] = input.nextInt();
}
System.out.print("\n");
}
System.out.print("matrix values \n");
// to show up the originally array
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.print("\n");
}
//////the new array to display only the even numbers in the col
System.out.print("\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 5; j++) {
int[] y = matrix[j];
for (int k = 0; i < y.length; i++) {
if (y[k] % 2 == 0)
System.out.println(y[k]);
}
}
}
}
}
the output doesn't print the new array
matrix values
1 2 3 4 5
6 7 8 9 10
3 2 4 5 9
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at Matrix.main(Matrix.java:43)
Replace i by k here :
for (int j = 0; j < 5; j++) {
int[] y = matrix[j];
for (int k = 0; i < y.length; i++) {
if (y[k] % 2 == 0)
System.out.println(y[k]);
}
}

Java creating a 2d int array which getting bigger inside

I want to create a 2d int array thats getting bigger from the outside to the inside, the first layer should start with 1 and the next layer should be 1 higher.
With n = 3 it should look like this :
1 1 1 1 1 1
1 2 2 2 2 1
1 2 3 3 2 1
1 2 3 3 2 1
1 2 2 2 2 1
1 1 1 1 1 1
thats what i already have
int n = 3;
int[][] feld = new int[2*n][2*n];
int c = 1 ;
for (int i = 0; i < 2*n; i++) {
for (int j = 0; j < 2*n; j++) {
feld[i][j] = c-i+j;
}
c++;
}
for (int i = 0; i < feld.length; i++) { //this to printing the matrix
for (int j = 0; j < feld[i].length; j++) {
System.out.print(feld[i][j] + " ");
}
System.out.println();
}
This should work.
int n = 3;
int[][] feld = new int[2*n][2*n];
int c = 0;
while(c<n) {
for(int i = c; i < 2*n-c; i++) {
feld[c][i] = c+1;
feld[i][c] = c+1;
feld[2*n-c-1][i] = c+1;
feld[i][2*n-c-1] = c+1;
}
c++;
}
for (int i = 0; i < feld.length; i++) { //print the matrix
for (int j = 0; j < feld[i].length; j++) {
System.out.print(feld[i][j] + " ");
}
System.out.println();
}
You can change n to whatever value and it should still work

Transposing a 2D Array Only Works For Rectangular Arrays - Java

I have a class with a method that transposes an array given the array, rows, and columns
public class Transpose {
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayRows][arrayColumns];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
}
The output I get looks like this:
Original:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
Transposed:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
I realized that the method only works when the values of arrayRows and arrayColumns are the same value, for example: 6, 6 or 5, 5. I tried to place the values opposite of each other, the rows in columns and vice versa, however it did not work. How can I get the method to work for non-rectangular/square arrays?
See line comments for explanation. Those are the only lines I modified.
public static void transpose(int[][] array, int arrayRows, int arrayColumns) {
int[][] transposedArray = new int[arrayColumns][arrayRows]; //swap number of rows and columns
System.out.println("Transposed:");
for (int i = 0; i < transposedArray.length; i++) { //take the length of transposedArray, not array
for (int j = 0; j < transposedArray[i].length; j++) { //take the length of transposedArray, not array
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
You need to swap the places of arrayRows and arrayColumns in the transposed matrix, because the new matrix is supposed to be a [5][6] instead of a [6][5].
So your line of
int[][] transposedArray = new int[arrayRows][arrayColumns];
becomes
int[][] transposedArray = new int[arrayColumns][arrayRows];
We also need to swap i and j in the following statement, because the loops are following the indices of the original matrix:
transposedArray[i][j] = array[j][i];
to
transposedArray[j][i] = array[i][j];
And lastly, you can't print the transposed matrix while you're creating it as you're doing now, because you're just re-printing the original matrix that way. I suggest printing the matrix after you're done creating it.
With these changes, your code ends up like this:
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayColumns][arrayRows];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[j][i] = array[i][j];
}
}
for(int i = 0; i < transposedArray.length; i++) { //print the transposed matrix
for(int j = 0; j < transposedArray[i].length; j++) {
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
You can see a working example here.
for(int i = 0; i < array.length; i++)
// should count the number of rows. = 6
iterating from 0 ... 5
for(int j = 0; j < array[i].length; j++) // should count the number of columns = 5
iterate from 0..4
transposedArray[i][j] = array[j][i];
to access 6 ... you need j=5 ; i = 0...4
your loops are not able to access the values

Need some help using Java with matrix

I am studying and have a little trouble.
I have to build matrix like this on the picture
Matrix
But i can't put a stars in that places.
public static void main(String[] args) {
int[][] twoD = new int[5][5];
int i, j, k = 1;
for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++) {
twoD[i][j] = k;
k++;
}
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++)
System.out.print(twoD[i][j] + " ");
System.out.print("\n");
}
}
Here is my code, please help to find
You can take the answer from a previous question and slightly modify it like so
public static void printCross(int size, char display)
{
int count = 0; //add a counter
for (int row = 0; row < size; row++) {
for (int col = 0; col < size; col++) {
count++; //increment each index we come across
if (row == col || row + col == size - 1) {
//print out the X using teh given display character
System.out.print(String.format("%-3s", display));
} else {
//print out the current count
System.out.print(String.format("%-3s", count));
}
}
System.out.println();
}
}
Output
X 2 3 4 X
6 X 8 X 10
11 12 X 14 15
16 X 18 X 20
X 22 23 24 X
I have found the easiest way i think.
public static void main(String args[]){
String[][] Matrix = { {" *"," 2"," 3"," 4"," *"} , {" 6"," *"," 8"," *","10"} , {"11","12"," *","14","15"} , {"16"," *","18"," *","20"} , {" *","22","23","24"," *"}};
for(int i=0; i< Matrix.length; i++){
for(int j=0;j < Matrix.length; j++){
System.out.print(Matrix[i][j]+" ");
}
System.out.println("");
}
}

Two Dimensional Array not printing values in matrix format [duplicate]

This question already has an answer here:
how to system.out.println on the same line(java) [duplicate]
(1 answer)
Closed 6 years ago.
I am trying to work on some 2 dimensional array examples. While I was trying to print the 2 dimensional array output in the following format
0 1 2
3 4 5
6 7 8
9 10 11
12 13 14
My output was showing up as like this
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Not exactly sure what's seems to be the problem
Here is my code:
public class TwoDArray {
public static void main(String[] args) {
int rows = 5;
int columns = 3;
int k = 0;
int[][] array = new int[rows][columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++) {
array[i][j] = k;
k++;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println(array[i][j] + " ");
}
System.out.println();
}
}
}
You have used :
System.out.println(number)
This will create a new line at the end of each number printed. To see output as you want it, you should use :
System.out.print(number)
CODE:
public class TwoDArray {
public static void main(String[] args) {
int rows = 5;
int columns = 3;
int k = 0;
int[][] array = new int[rows][columns];
for (int i = 0; i < rows; i++)
for (int j = 0; j < columns; j++) {
array[i][j] = k;
k++;
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}
}
}
You are printing new line every time you print an element.
Use this code. It should solve your problem:
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println();
}

Categories

Resources