I need to construct a method that receives a matrix as argument and prints its lines. For example, the method will receive the matrix below
int[][] matrix = {
{3, 1, 2, 7, 2, 5, 6, 2},
{2, 10, 20, 12, 13},
{3, 7, 12, 15, 18, 4},
{2, 11, 21, 12, 13}
};
And it must return
Line 0 : 3 1 2 7 2 5 6 2
Line 1 : 2 10 20 12 13
Line 2 : 3 7 12 15 18 4
Line 3 : 2 11 21 12 13
Here is the method:
public static void displayLines(int[][] matrix){
for (int i = 0; i < matrix.length; ++i){
for (int j = 0; j < matrix[i].length; ++j){
System.out.print("Line nb " + i + " : " + matrix[i][j] + " ");
}
}
}
But it does not print in the way I want. It prints like that and I'm trying to figure out how to solve it.
Line 0 : 3
Line 0 : 1
Line 0 : 2
Line 0 : 7
Line 0 : 2
Line 0 : 5
Line 0 : 6
Line 0 : 2
// And so on for other lines
You are calling System.out.print for each element of the array instead of each row of the array. Modify like this :
public static void displayLines(int[][] matrix){
for (int i = 0; i < matrix.length; ++i){
System.out.print("Line " + i + " :"); // line header
for (int j = 0; j < matrix[i].length; ++j){
System.out.print(" " + matrix[i][j]);
}
System.out.println(""); // end of line
}
}
Note how an empty space is added before each element " " + matrix[i][j] to avoid trailing whitespace.
Also bear in mind you could write the second for loop like this
for (int n : matrix[i]) {
System.out.print(" " + n);
}
How about something like:
int rowNumber = 0;
for (int[] row : matrix){
System.out.println("Line " + (rowNumber++) + " : " + Arrays.toString(row));
}
which will generate
Line 0 : [3, 1, 2, 7, 2, 5, 6, 2]
Line 1 : [2, 10, 20, 12, 13]
Line 2 : [3, 7, 12, 15, 18, 4]
Line 3 : [2, 11, 21, 12, 13]
Use the code below
public static void displayLines(int[][] matrix){
for (int i = 0; i < matrix.length; ++i){
System.out.print("Line nb " + i + " : " );
for (int j = 0; j < matrix[i].length; ++j){
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void displayLines(int[][] matrix) {
for (int i = 0; i < matrix.length; ++i) {
System.out.print("Line " + i + " : ");
for (int j = 0; j < matrix[i].length; ++j) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
int[][] matrix = { { 3, 1, 2, 7, 2, 5, 6, 2 }, { 2, 10, 20, 12, 13 },
{ 3, 7, 12, 15, 18, 4 }, { 2, 11, 21, 12, 13 } };
for (int i = 0; i < matrix.length; ++i) {
System.out.print("Line " + i + " :");
for (int j = 0; j < matrix[i].length; ++j) {
System.out.print(matrix[i][j] + " ");
}
System.out.println("");
}
Related
Notice how the '0' of the collisions column in the 3rd line is shifted, as well as other numbers, what is a fix to that? [
This is my current output
System.out.println(""+capacity +"\t\t\t "+size()+"\t\t "+num+"\t\t"+ data + "\t\t\t" + collisions)
I would start by precomputing the lengths of all of the header fields. Then use that and formatted io to build a table programmatically. Something like,
String[] headings = {
"Capacity",
"Size",
"Num",
"data",
"Collisions"
};
int[] lengths = new int[headings.length];
for (int i = 0; i < headings.length; i++) {
lengths[i] = headings[i].length() + 4;
}
int[][] values = {
{ 100, 1, 1, 241, 0 },
{ 100, 2, 1, 289, 0 },
{ 100, 3, 1, 4, 0 }
};
for (int i = 0; i < headings.length; i++) {
System.out.printf("%-" + lengths[i] + "s", headings[i]);
}
System.out.println();
for (int i : lengths) {
for (int j = 0; j < i; j++) {
System.out.print("=");
}
}
System.out.println();
for (int[] arr : values) {
for (int i = 0; i < arr.length; i++) {
System.out.printf("%-" + lengths[i] + "s", arr[i]);
}
System.out.println();
}
Outputs
Capacity Size Num data Collisions
=================================================
100 1 1 241 0
100 2 1 289 0
100 3 1 4 0
I have recently started to learn java. I have a task, where I have to print a number grid with specific values in it.
Heres the code:
public static void main(String[] args) {
int array[][] = {{1, 2, 3, 5, 7},
{10, 11, 12, 14, 16},
{19, 20, 21, 23, 25},
{28, 29, 30, 32, 34},
{37, 38, 39, 41, 43},
{46, 47, 48, 50, 52}};
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++) {
System.out.print(array[i][j] + " ");
}
System.out.println("");
}
}
}
Now it works perfectly fine, but ideally I could only declare values of the first row and use for loop with if statement to accomplish the same thing, yet I struggle to figure out code for that.
The difference between the two elements in a column is 9: a[i][j] = a[i-1][j] + 9, thus the code may be modified as:
public static void main(String args[]) {
int array[][] = new int[6][5];
// initialize the first row
array[0] = new int[] {1, 2, 3, 5, 7};
// print the first row
System.out.println(Arrays.toString(array[0]));
for (int i = 1; i < array.length; i++) {
// populate current row incrementing each element by 9
for (int j = 0; j < array[i].length; j++) {
array[i][j] = array[i - 1][j] + 9;
}
System.out.println(Arrays.toString(array[i]));
}
}
Output:
[1, 2, 3, 5, 7]
[10, 11, 12, 14, 16]
[19, 20, 21, 23, 25]
[28, 29, 30, 32, 34]
[37, 38, 39, 41, 43]
[46, 47, 48, 50, 52]
Update
It would be more interesting task to generate the sequence starting from 1.
Inside the row the differences between two adjacent elements change like 1, 1, 2, 2, 3, and 3 is the difference between the last element of the previous row and the first element in the next row.
int x = 1;
for (int i = 1; i < 7; i++) {
for (int j = 1; j < 6; j++) {
System.out.print(x + " ");
x += j / 2 + j % 2;
}
System.out.println();
}
I want to print out an increasing number of elements in my array per line but I'm not sure how I could do it.
public static void main(String[] args) {
int[] x = new int[21];
for (int i = 0; i < x.length; i++) {
x[i] = i + 1;
}
System.out.println(Arrays.toString(x));
}
I would like my output to look like:
[1]
[2, 3]
[4, 5, 6]
etc...
instead of what I get right now which is
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]
I'm really new to java so any tips would really be appreciated, thanks.
Add this below your code.
for (int i = 0, ctr = 0; i < x.length; ctr++) {
System.out.print("[ ");
for (int j = 0; j <= ctr; i++) {
System.out.print(x[i]);
j++;
if (j <= ctr) {
System.out.print(" ,");
}
}
System.out.println(" ]");
}
This method does not require storage
int start = 1;
int count = 1;
int outer = 6;
for (int y = 0; y < outer; y++) {
System.out.print ("[");
int x = start;
for (; x < start + count; x++) {
System.out.print (x);
if (x < start + count - 1)
System.out.print(",");
}
System.out.println ("]");
count++;
start = x;
}
result
[1]
[2,3]
[4,5,6]
[7,8,9,10]
[11,12,13,14,15]
[16,17,18,19,20,21]
You can use this code
int[] x = new int[21];
for (int i = 0; i < x.length; i++) {
x[i] = i + 1;
}
int start = 0, len = 1;
while(start + len <= x.length) {
int[] newArray = Arrays.copyOfRange(x, start, start + len);
System.out.println(Arrays.toString(newArray));
start += len;
len++;
}
Using two loops you can achieve the result, the outer loop will create an empty array with each iteration and the inner one will populate it with numbers. Also using a third variable to keep track of the last number generated.
public static void main(String[] args) {
int n = 21;
int lastNumber = 0;
int x[] = null;
for(int j = 0; j< n; j++) {
x = new int[j];
for (int i = 0, k = lastNumber; i< j; i++,k++) {
x[i] = k + 1;
}
if(x.length != 0){
lastNumber = x[x.length - 1];
System.out.println(Arrays.toString(x));
}
}
}
Output:
[1]
[2, 3]
[4, 5, 6]
[7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20, 21]
I want the output to look like:
[1, 1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
[1, 1, 1, 1]
My code right now outputs:
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
for (int j = 0; j < Matrix.length; j++) {
for (int k = 0; k < Matrix[0].length; k++) {
System.out.print(Matrix[j][k] + " ");
}
System.out.print("\n");
}
How can I make this work?
Try this
for (int[] row : Matrix)
System.out.println(Arrays.toString(row));
Just print commas and brackets around your loop and after the number:
for (int j = 0; j < Matrix.length; j++) {
System.out.print("[");
for (int k = 0; k < Matrix[0].length; k++) {
System.out.print(Matrix[j][k] + " ");
if (k + 1 < Matrix[0].length) System.out.print(", ")
}
System.out.print("]\n");
}
Print a [ before your first line and a ] after your last. Then add a comma before the space. Also, Java variable names start with a lower case letter (by convention). You could also use a for-each loop and something like,
int[][] matrix = { { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 1 } };
for (int[] array : matrix) {
System.out.print("[");
for (int i = 0; i < array.length; i++) {
if (i != 0) {
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println("]");
}
which will produce your requested output (but will also handle jagged arrays).
for(i=0;i<row_count;i++) {
for(j=0;j<column_count;j++) {
System.out.print(Matrix[i][j]+" ");
}
System.out.print("\n");
}
I have to output the array with a maximum of 4 array values per line, but I can't figure out how to convert it to a 2 dimensional array. After the dashes is where I am having trouble. If I don't output it as a 2D array, how else would I restrict it to have only 4 values per line?
public class arrayExampleB{
public static void main(String[] args){
int[] x = {22, 12, 28, 4, 30, 59, 17, 82, 1, 99, 47, 2, 8, 20, 80};
System.out.print("Pre-Swapped Array Set (linear): {");
for(int i=0; i<=x.length-1; i++){
if(i<x.length-1){
System.out.print(x[i] + ", ");
}
else{System.out.print(x[i]);}
}
System.out.print("}");
int y = x.length-1;
int temp = x[y];
x[y] = x[1];
x[1] = temp;
int z = x.length-2;
int temp2 = x[z];
x[z] = x[0];
x[0] = temp2;
System.out.print("\nPost-Swapped Array Set (linear): {");
for(int i=0; i<=x.length-1; i++){
if(i<x.length-1){
System.out.print(x[i] + ", ");
}
else{System.out.print(x[i]);}
}
System.out.print("}");
//-------------------------------------------------------------
int d = (x.length / 4) + (x.length % 4);
int i = 0;
int j = 0;
int[][] t = new int[i][j];
System.out.print("\nPre-Swapped Array Set (2D): {");
for(i=0; i <= 4; i++){
for(j=0; j < d; j++){
System.out.print(t[i][j] + " ");
}
System.out.println();
}
System.out.print("}");
}
}
To output a 1d array as 2d with a max of 4 values on a line, use this code:
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
if ((i+1) % 4 == 0)
System.out.println();
}
int[] x = {22, 12, 28, 4, 30, 59, 17, 82, 1, 99, 47, 2, 8, 20, 80};
int[][] t = new int[4][4];
// populate 2D
int k = 0
for(i=0; i <= t.length; i++){
for(j=0; j < t[i].length; j++){
t[i][j] = x[k];
k++l
}
}
// print
for(i=0; i <= t.length; i++){
System.out.print("{");
for(j=0; j < t[i].length; j++){
System.out.print(t[i][j]);
}
System.out.println("}");
}
Without taking too close a look on your code: To output a one dimensional array on several lines on the console consider this:
int[] x = {22, 12, 28, 4, 30, 59, 17, 82, 1, 99, 47, 2, 8, 20, 80};
for(int i = 0; i < x.length; i++)
{
System.out.print(x[i] + ' ');
if( (i+1) % 4 == 0)
System.out.print('\n');
}