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 ) );
}
}
}









share|improve this question
























  • 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















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 ) );
}
}
}









share|improve this question
























  • 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













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 ) );
}
}
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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));





share|improve this answer























  • 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 use addAll() method. add() is used for appending just one element to the list.
    – Hülya
    Nov 11 at 18:05













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























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));





share|improve this answer























  • 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 use addAll() method. add() is used for appending just one element to the list.
    – Hülya
    Nov 11 at 18:05

















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));





share|improve this answer























  • 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 use addAll() method. add() is used for appending just one element to the list.
    – Hülya
    Nov 11 at 18:05















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));





share|improve this answer














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));






share|improve this answer














share|improve this answer



share|improve this answer








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 use addAll() 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






  • 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


















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




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

How to pass form data using jquery Ajax to insert data in database?

National Museum of Racing and Hall of Fame

Guess what letter conforming each word