Consider it as an array of arrays and this will work for sure.
int mat[][] = { {10, 20, 30, 40, 50, 60, 70, 80, 90},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50, 51, 89},
};
for(int i=0; i<mat.length; i++) {
for(int j=0; j<mat[i].length; j++) {
System.out.println("Values at arr["+i+"]["+j+"] is "+mat[i][j]);
}
}
Answer from Bharat on Stack OverflowConsider it as an array of arrays and this will work for sure.
int mat[][] = { {10, 20, 30, 40, 50, 60, 70, 80, 90},
{15, 25, 35, 45},
{27, 29, 37, 48},
{32, 33, 39, 50, 51, 89},
};
for(int i=0; i<mat.length; i++) {
for(int j=0; j<mat[i].length; j++) {
System.out.println("Values at arr["+i+"]["+j+"] is "+mat[i][j]);
}
}
Just invert the indexes' order like this:
for (int j = 0; j<array[0].length; j++){
for (int i = 0; i<array.length; i++){
because all rows has same amount of columns you can use this condition j < array[0].lengt in first for condition due to the fact you are iterating over a matrix
Java - How to populate 2d array with nested while loops? - Stack Overflow
Homework - Do While loop with 2d array
i and j are local to the for loops, you should declare i and j outside of the for loops at the top if you want to use them with a do while loop. This allows for a greater scope of the program
More on reddit.comjava - How to make while loop work properly to correct given 2D array - Stack Overflow
Multi-dimensional arrays topic in java
Hello,
I'm trying to finish up an assignment and am in need of some direction. The problem is to ask a user to enter some data for some students. The user will enter the number of classes left to graduate (1-20) and how many courses each term (1-5).
I currently have the program setup to accept any range of data and it is working no problems, but i'm not sure how I need to go about setting up the do/while loop properly. So far I have:
import java.util.Scanner;
public class Chpt8_Project {
public static void main(String\[\] args) {
Scanner input = new Scanner([System.in](https://System.in));
double\[\]\[\] students = new double\[4\]\[2\];
// Enter number of courses left to graduate(1-20) and how many courses to take each term
for (int i = 0; i < students.length; i++) {
System.out.print("Enter classes remaining and taking each term for student " + (i+1) + ": ");
for (int j = 0; j < students\[i\].length; j++) {students[i][j] = input.nextDouble();
} }
I've tried to add a do / while loop with the do part before the 1st for loop but when I attempt to add the while condition it tells me the variables i and j don't exist. Any tips about how to tackle this problem will be greatly apprieciated.
Thanks.
i and j are local to the for loops, you should declare i and j outside of the for loops at the top if you want to use them with a do while loop. This allows for a greater scope of the program
a while loop that mimics your for loop might look like:
int i = 0;
while (i < students.length) {
// do something
i++;
}
a do/while might look like:
int i = 0;
do {
// do something
i++;
} while (i < student.length);
I've been working through the hyperskills.org java track and have gotten hung up on multidimensional arrays. I thought maybe I missed something on a previous topic so I have gone back and reviewed for-loops and arrays, which I think I have a good grip on now. Still when I come back and look at the code practicals for working with md arrays I'm still feeling just as confused as when I first encountered them. Surely I'm over-complicating the matter. Any suggestions on moving forward?
Assuming you only have to initialize the cells that get 1 value (the rest of the cells are 0 by default):
If a is much smaller than n, you can initialize the cells that get 1 value in O(n + a*n) time.
For example, if a == 0, all you need is to set the n cells of the main diagonal of the matrix ((0,0),(1,0),...,(n-1,n-1)) to 1.
If a == 1, you need to set the n cells of the main diagonal + the 2*(n-1) cells of the diagonal adjacent to the main diagonal.
...
If a = c, you need to set O(n) + O(2c*n) cells to 1, which is O(n + c*n).
To implement this algorithm, you'll need to replace your O(n^2) loop with 2*a+1 O(n) loops (one loop for each relevant diagonal).
I think that you use the wrong tool for your problem. Not every problem is a nail, and so not every solution involves a hammer. If you create a Matrix interface, and program against that interface, you can solve the instantiation in O(1) and also use less memory:
interface Matrix {
int get(int i, int j);
}
class OrdinaryMatrix implements Matrix {
int[][] data;
public OrdinaryMatrix (int rows, int columns) { ... }
public int get(int i, int j) {
return data[i][j];
}
}
class SpecialMatrix implements Matrix {
private final int a;
public SpecialMatrix (int rows, int columns, int a) {
...
this.a = a;
}
public int get(int i, int j) {
return Math.abs(i-j)<=a ? 1 : 0
}
}