Reputation: 71
So I have created a 2D field that is filled with identifiers by group of threes.
Is there a way to start iterating for example from 4th row? if yes how is it done? Here is an image of 2d field.
I tried for each loop but it seems it won't work the right way.
This is the code, where I can't figure out, how to get 4th row for example.
Upvotes: 0
Views: 753
Reputation: 1
If you only want to get a particular row you can change the outer loop to basic for loop as already mentioned. as shown:-
private Integer[][] comparedProducts = new Integer[][]{
{1,2,3},
{4,5,6},
{7,8,9},
{10,11,12}
};
for(int i=3;i<size;i++){
for(int a:comparedProducts[i]){
//DO work
}
}
Also, you can can use ArrayList instead of array and then you can use the subList method of ArrayList.
List<List<Integer>> comparedProducts = new ArrayList<>();
public void initialiseList(){
comparedProducts.add(Arrays.asList(1,2,3));
comparedProducts.add(Arrays.asList(4,5,6));
comparedProducts.add(Arrays.asList(7,8,9));
}
for(List comparedProduct:comparedProducts.subList(3,comparedProducts.size()){
// One more foreach loop for each element
// Or do some Work.
}
ArrayList has a lot of really helpful methods. So if it's not much change use ArrayList.
Upvotes: 0
Reputation: 1767
In this situation you have two choices. Traditional you can use a ‘for' loop something like this:
for (int i=0, i<10, i++) {
*commands here*
}
int i=0
is your initial condition here you could replace 0 with the index you wish to start from. Remember array indexes start at 0 so the 4th element would be index 3
i<10
controls how many times you wish to loop, in this case 10 times.
i++
is how you wish to increment your index value
Of course you could use a while
but for
loops are generally better in this case. I would suggest looking them up as they have some useful features I haven’t covered here.
Your second choice is to use streams:
Arrays.toStream(myArray)
.drop(4) // drop the first four elements
.other processing
Streams can be less “efficient” in some cases but I find them more readable.
Upvotes: 1
Reputation: 1098
I think this will work
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=3; 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]);
}
}
Upvotes: 2
Reputation: 161
Yes you can do it. Start the iteration by specifying the initial loop to 3 if you want to start it from the 4th row.
for(int i=3 ; i < (maximum number of rows initiated at the beginning of the array); i++)
In general the code can be as follows
for(i=the row which you want to start from; i < maximum number of rows initiated at the beginning of the array; i++){
for(j=0; j<maximum number of columns initiated at the beginning of the array; j++)
{
System.out.println(arr[i][j]);
}
}
Upvotes: 1
Reputation: 91
sorry your image is missing, but I'd suggest something like this, pick the index in row loop
int startRow = 2;
for (int i = startRow; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
board[i][j] = i + j;
}
}
Upvotes: 3
Reputation: 1592
Instead of using foreach use the classical for loop with starting index as 3.
for(i=3; i<row; i++)
{
for(j=0; j<col; j++)
{
arr[i][j] = scan.nextInt();
}
}
Upvotes: 3