Remove multiple row from array by looping
up vote
0
down vote
favorite
I have 2d array (test) and 2d array of number of row needs to be removed (remove Row). The problem is the loop I create resulting in no result when I try to print the test array. I need to create new matrix that not include row 0,1,3,4.
How can I access the new array outside the while loop?
import java.util.ArrayList;
import java.util.Arrays;
public class remove
{
public static void main( String args )
{
double test = { { 100 }, { 200 }, { 300 }, { 400 }, { 500 }, { 600 }, { 700 },
{ 800 }, { 900 }, { 1000 } };
int removeRow = { { 0 }, { 1 }, { 3 }, { 4 }, };
int reduce = 1;
int r = 0;
while ( reduce == 0 )
{
ArrayList<double> rowsToKeep = new ArrayList<double>( test.length );
for ( int i = 0; i < test.length; i++ )
{
if ( i != removeRow[0][0] - r )
{
double row = test[i];
rowsToKeep.add( row );
}
}
r++;
test = new double[rowsToKeep.size()];
for ( int i = 0; i < rowsToKeep.size(); i++ )
{
test[i] = rowsToKeep.get( i );
}
reduce = removeRow.length - r;
r = r++;
System.out.println( Arrays.deepToString( test ) );
}
}
}
java arrays
add a comment |
up vote
0
down vote
favorite
I have 2d array (test) and 2d array of number of row needs to be removed (remove Row). The problem is the loop I create resulting in no result when I try to print the test array. I need to create new matrix that not include row 0,1,3,4.
How can I access the new array outside the while loop?
import java.util.ArrayList;
import java.util.Arrays;
public class remove
{
public static void main( String args )
{
double test = { { 100 }, { 200 }, { 300 }, { 400 }, { 500 }, { 600 }, { 700 },
{ 800 }, { 900 }, { 1000 } };
int removeRow = { { 0 }, { 1 }, { 3 }, { 4 }, };
int reduce = 1;
int r = 0;
while ( reduce == 0 )
{
ArrayList<double> rowsToKeep = new ArrayList<double>( test.length );
for ( int i = 0; i < test.length; i++ )
{
if ( i != removeRow[0][0] - r )
{
double row = test[i];
rowsToKeep.add( row );
}
}
r++;
test = new double[rowsToKeep.size()];
for ( int i = 0; i < rowsToKeep.size(); i++ )
{
test[i] = rowsToKeep.get( i );
}
reduce = removeRow.length - r;
r = r++;
System.out.println( Arrays.deepToString( test ) );
}
}
}
java arrays
does your while loop execute? i don't see how it can
– 4dc0
Nov 11 at 16:10
It doesn't show any error when run in eclipse. But there no value print from the array. I don't know any other method that suitable for my my problem.
– Harith
Nov 11 at 16:18
You only access removeRow[0][0] , why is it an array, when you never use any other elements?
– fairtrax
Nov 11 at 16:50
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have 2d array (test) and 2d array of number of row needs to be removed (remove Row). The problem is the loop I create resulting in no result when I try to print the test array. I need to create new matrix that not include row 0,1,3,4.
How can I access the new array outside the while loop?
import java.util.ArrayList;
import java.util.Arrays;
public class remove
{
public static void main( String args )
{
double test = { { 100 }, { 200 }, { 300 }, { 400 }, { 500 }, { 600 }, { 700 },
{ 800 }, { 900 }, { 1000 } };
int removeRow = { { 0 }, { 1 }, { 3 }, { 4 }, };
int reduce = 1;
int r = 0;
while ( reduce == 0 )
{
ArrayList<double> rowsToKeep = new ArrayList<double>( test.length );
for ( int i = 0; i < test.length; i++ )
{
if ( i != removeRow[0][0] - r )
{
double row = test[i];
rowsToKeep.add( row );
}
}
r++;
test = new double[rowsToKeep.size()];
for ( int i = 0; i < rowsToKeep.size(); i++ )
{
test[i] = rowsToKeep.get( i );
}
reduce = removeRow.length - r;
r = r++;
System.out.println( Arrays.deepToString( test ) );
}
}
}
java arrays
I have 2d array (test) and 2d array of number of row needs to be removed (remove Row). The problem is the loop I create resulting in no result when I try to print the test array. I need to create new matrix that not include row 0,1,3,4.
How can I access the new array outside the while loop?
import java.util.ArrayList;
import java.util.Arrays;
public class remove
{
public static void main( String args )
{
double test = { { 100 }, { 200 }, { 300 }, { 400 }, { 500 }, { 600 }, { 700 },
{ 800 }, { 900 }, { 1000 } };
int removeRow = { { 0 }, { 1 }, { 3 }, { 4 }, };
int reduce = 1;
int r = 0;
while ( reduce == 0 )
{
ArrayList<double> rowsToKeep = new ArrayList<double>( test.length );
for ( int i = 0; i < test.length; i++ )
{
if ( i != removeRow[0][0] - r )
{
double row = test[i];
rowsToKeep.add( row );
}
}
r++;
test = new double[rowsToKeep.size()];
for ( int i = 0; i < rowsToKeep.size(); i++ )
{
test[i] = rowsToKeep.get( i );
}
reduce = removeRow.length - r;
r = r++;
System.out.println( Arrays.deepToString( test ) );
}
}
}
java arrays
java arrays
edited Nov 11 at 17:23
Gayan Mettananda
46037
46037
asked Nov 11 at 16:06
Harith
113
113
does your while loop execute? i don't see how it can
– 4dc0
Nov 11 at 16:10
It doesn't show any error when run in eclipse. But there no value print from the array. I don't know any other method that suitable for my my problem.
– Harith
Nov 11 at 16:18
You only access removeRow[0][0] , why is it an array, when you never use any other elements?
– fairtrax
Nov 11 at 16:50
add a comment |
does your while loop execute? i don't see how it can
– 4dc0
Nov 11 at 16:10
It doesn't show any error when run in eclipse. But there no value print from the array. I don't know any other method that suitable for my my problem.
– Harith
Nov 11 at 16:18
You only access removeRow[0][0] , why is it an array, when you never use any other elements?
– fairtrax
Nov 11 at 16:50
does your while loop execute? i don't see how it can
– 4dc0
Nov 11 at 16:10
does your while loop execute? i don't see how it can
– 4dc0
Nov 11 at 16:10
It doesn't show any error when run in eclipse. But there no value print from the array. I don't know any other method that suitable for my my problem.
– Harith
Nov 11 at 16:18
It doesn't show any error when run in eclipse. But there no value print from the array. I don't know any other method that suitable for my my problem.
– Harith
Nov 11 at 16:18
You only access removeRow[0][0] , why is it an array, when you never use any other elements?
– fairtrax
Nov 11 at 16:50
You only access removeRow[0][0] , why is it an array, when you never use any other elements?
– fairtrax
Nov 11 at 16:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I changed your algorithm a little bit. Here if (j < removeRow.length && removeRow[j][0] == i) we find the row to remove and do nothing just increment j otherwise in else block add the row in rowsToKeep list.
Try this:
double test = { {100}, {200}, {300}, {400}, {500}, {600}, {700}, {800}, {900}, {1000} };
int removeRow = { {0}, {1}, {3}, {4} };
int j = 0;
List<double> rowsToKeep = new ArrayList<>(test.length);
for (int i = 0; i < test.length; i++) {
if (j < removeRow.length && removeRow[j][0] == i) {
j++;
} else {
double row = test[i];
rowsToKeep.add(row);
}
}
test = new double[rowsToKeep.size()];
for (int i = 0; i < rowsToKeep.size(); i++) {
test[i] = rowsToKeep.get(i);
}
System.out.println(Arrays.deepToString(test));
i want to ask question regarding .add(row). Can we store more than one row? I just learned recently about arraylist and want to know about meaning of .add, .size and .get
– Harith
Nov 11 at 17:54
1
For more than one row you can useaddAll()method.add()is used for appending just one element to the list.
– Hülya
Nov 11 at 18:05
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I changed your algorithm a little bit. Here if (j < removeRow.length && removeRow[j][0] == i) we find the row to remove and do nothing just increment j otherwise in else block add the row in rowsToKeep list.
Try this:
double test = { {100}, {200}, {300}, {400}, {500}, {600}, {700}, {800}, {900}, {1000} };
int removeRow = { {0}, {1}, {3}, {4} };
int j = 0;
List<double> rowsToKeep = new ArrayList<>(test.length);
for (int i = 0; i < test.length; i++) {
if (j < removeRow.length && removeRow[j][0] == i) {
j++;
} else {
double row = test[i];
rowsToKeep.add(row);
}
}
test = new double[rowsToKeep.size()];
for (int i = 0; i < rowsToKeep.size(); i++) {
test[i] = rowsToKeep.get(i);
}
System.out.println(Arrays.deepToString(test));
i want to ask question regarding .add(row). Can we store more than one row? I just learned recently about arraylist and want to know about meaning of .add, .size and .get
– Harith
Nov 11 at 17:54
1
For more than one row you can useaddAll()method.add()is used for appending just one element to the list.
– Hülya
Nov 11 at 18:05
add a comment |
up vote
1
down vote
accepted
I changed your algorithm a little bit. Here if (j < removeRow.length && removeRow[j][0] == i) we find the row to remove and do nothing just increment j otherwise in else block add the row in rowsToKeep list.
Try this:
double test = { {100}, {200}, {300}, {400}, {500}, {600}, {700}, {800}, {900}, {1000} };
int removeRow = { {0}, {1}, {3}, {4} };
int j = 0;
List<double> rowsToKeep = new ArrayList<>(test.length);
for (int i = 0; i < test.length; i++) {
if (j < removeRow.length && removeRow[j][0] == i) {
j++;
} else {
double row = test[i];
rowsToKeep.add(row);
}
}
test = new double[rowsToKeep.size()];
for (int i = 0; i < rowsToKeep.size(); i++) {
test[i] = rowsToKeep.get(i);
}
System.out.println(Arrays.deepToString(test));
i want to ask question regarding .add(row). Can we store more than one row? I just learned recently about arraylist and want to know about meaning of .add, .size and .get
– Harith
Nov 11 at 17:54
1
For more than one row you can useaddAll()method.add()is used for appending just one element to the list.
– Hülya
Nov 11 at 18:05
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I changed your algorithm a little bit. Here if (j < removeRow.length && removeRow[j][0] == i) we find the row to remove and do nothing just increment j otherwise in else block add the row in rowsToKeep list.
Try this:
double test = { {100}, {200}, {300}, {400}, {500}, {600}, {700}, {800}, {900}, {1000} };
int removeRow = { {0}, {1}, {3}, {4} };
int j = 0;
List<double> rowsToKeep = new ArrayList<>(test.length);
for (int i = 0; i < test.length; i++) {
if (j < removeRow.length && removeRow[j][0] == i) {
j++;
} else {
double row = test[i];
rowsToKeep.add(row);
}
}
test = new double[rowsToKeep.size()];
for (int i = 0; i < rowsToKeep.size(); i++) {
test[i] = rowsToKeep.get(i);
}
System.out.println(Arrays.deepToString(test));
I changed your algorithm a little bit. Here if (j < removeRow.length && removeRow[j][0] == i) we find the row to remove and do nothing just increment j otherwise in else block add the row in rowsToKeep list.
Try this:
double test = { {100}, {200}, {300}, {400}, {500}, {600}, {700}, {800}, {900}, {1000} };
int removeRow = { {0}, {1}, {3}, {4} };
int j = 0;
List<double> rowsToKeep = new ArrayList<>(test.length);
for (int i = 0; i < test.length; i++) {
if (j < removeRow.length && removeRow[j][0] == i) {
j++;
} else {
double row = test[i];
rowsToKeep.add(row);
}
}
test = new double[rowsToKeep.size()];
for (int i = 0; i < rowsToKeep.size(); i++) {
test[i] = rowsToKeep.get(i);
}
System.out.println(Arrays.deepToString(test));
edited Nov 12 at 11:31
buræquete
5,08141747
5,08141747
answered Nov 11 at 17:10
Hülya
45219
45219
i want to ask question regarding .add(row). Can we store more than one row? I just learned recently about arraylist and want to know about meaning of .add, .size and .get
– Harith
Nov 11 at 17:54
1
For more than one row you can useaddAll()method.add()is used for appending just one element to the list.
– Hülya
Nov 11 at 18:05
add a comment |
i want to ask question regarding .add(row). Can we store more than one row? I just learned recently about arraylist and want to know about meaning of .add, .size and .get
– Harith
Nov 11 at 17:54
1
For more than one row you can useaddAll()method.add()is used for appending just one element to the list.
– Hülya
Nov 11 at 18:05
i want to ask question regarding .add(row). Can we store more than one row? I just learned recently about arraylist and want to know about meaning of .add, .size and .get
– Harith
Nov 11 at 17:54
i want to ask question regarding .add(row). Can we store more than one row? I just learned recently about arraylist and want to know about meaning of .add, .size and .get
– Harith
Nov 11 at 17:54
1
1
For more than one row you can use
addAll() method. add() is used for appending just one element to the list.– Hülya
Nov 11 at 18:05
For more than one row you can use
addAll() method. add() is used for appending just one element to the list.– Hülya
Nov 11 at 18:05
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53250581%2fremove-multiple-row-from-array-by-looping%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
does your while loop execute? i don't see how it can
– 4dc0
Nov 11 at 16:10
It doesn't show any error when run in eclipse. But there no value print from the array. I don't know any other method that suitable for my my problem.
– Harith
Nov 11 at 16:18
You only access removeRow[0][0] , why is it an array, when you never use any other elements?
– fairtrax
Nov 11 at 16:50