Java How to remove item from nested Array ListHow do I check if a list is empty?Create ArrayList from arrayFinding the index of an item given a list containing it in PythonHow to randomly select an item from a list?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to make a flat list out of list of listsHow do I determine whether an array contains a particular value in Java?How to clone or copy a list?How do I convert a String to an int in Java?
Why do Russians call their women expensive ("дорогая")?
Which noble houses were destroyed during the Game of Thrones?
Modern approach to radio buttons
How to extract lower and upper bound in numeric format from a confidence interval string?
What is the 中 in ダウンロード中?
Is this story about US tax office reasonable?
Mother abusing my finances
How many chess players are over 2500 Elo?
How to properly maintain eye contact with people that have distinct facial features?
How do I subvert the tropes of a train heist?
What does "Marchentalender" on the front of a postcard mean?
What is the difference between nullifying your vote and not going to vote at all?
How to prevent bad sectors?
If a massive object like Jupiter flew past the Earth how close would it need to come to pull people off of the surface?
Should I use n only, b only, bg, bgn, or gn?
What is a subpixel in Super Mario Bros, and how does it relate to wall clipping?
Smart people send dumb people to a new planet on a space craft that crashes into a body of water
What does it mean when you think without speaking?
Uses of T extends U?
Split polygon using another polygon in QGIS
What F1 in name of seeds/varieties means?
Yandex Programming Contest: Alarms
Why does the UK have more political parties than the US?
Looking after a wayward brother in mother's will
Java How to remove item from nested Array List
How do I check if a list is empty?Create ArrayList from arrayFinding the index of an item given a list containing it in PythonHow to randomly select an item from a list?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to make a flat list out of list of listsHow do I determine whether an array contains a particular value in Java?How to clone or copy a list?How do I convert a String to an int in Java?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a nested ArrayList as below with boolean values. I want to remove for example 3rd item from all the rows. I tried with a loop but it doesnt resolve remove
as a method. How should I do this? Many thanks for your help.
for (int i = 0; i < list.size(); i++)
list.get(i).remove(3)// this remove method shows as an error in IDE
true false true false false false
false false true false true true
java list nested nested-lists
add a comment |
I have a nested ArrayList as below with boolean values. I want to remove for example 3rd item from all the rows. I tried with a loop but it doesnt resolve remove
as a method. How should I do this? Many thanks for your help.
for (int i = 0; i < list.size(); i++)
list.get(i).remove(3)// this remove method shows as an error in IDE
true false true false false false
false false true false true true
java list nested nested-lists
what's the type oflist
? perhaps you are using raw types.
– Eran
Mar 24 at 8:40
your list look likeList<List<Boolean>> list
– YCF_L
Mar 24 at 8:40
@Eran It's a list of List<Instance> listInstances = new ArrayList<Instance>(); and the class Instance has vals = new ArrayList<Boolean>(); Let me know if it explains a bit
– user4046073
Mar 24 at 8:44
add a comment |
I have a nested ArrayList as below with boolean values. I want to remove for example 3rd item from all the rows. I tried with a loop but it doesnt resolve remove
as a method. How should I do this? Many thanks for your help.
for (int i = 0; i < list.size(); i++)
list.get(i).remove(3)// this remove method shows as an error in IDE
true false true false false false
false false true false true true
java list nested nested-lists
I have a nested ArrayList as below with boolean values. I want to remove for example 3rd item from all the rows. I tried with a loop but it doesnt resolve remove
as a method. How should I do this? Many thanks for your help.
for (int i = 0; i < list.size(); i++)
list.get(i).remove(3)// this remove method shows as an error in IDE
true false true false false false
false false true false true true
java list nested nested-lists
java list nested nested-lists
edited Mar 24 at 9:44
YCF_L
34.8k104687
34.8k104687
asked Mar 24 at 8:37
user4046073user4046073
3371417
3371417
what's the type oflist
? perhaps you are using raw types.
– Eran
Mar 24 at 8:40
your list look likeList<List<Boolean>> list
– YCF_L
Mar 24 at 8:40
@Eran It's a list of List<Instance> listInstances = new ArrayList<Instance>(); and the class Instance has vals = new ArrayList<Boolean>(); Let me know if it explains a bit
– user4046073
Mar 24 at 8:44
add a comment |
what's the type oflist
? perhaps you are using raw types.
– Eran
Mar 24 at 8:40
your list look likeList<List<Boolean>> list
– YCF_L
Mar 24 at 8:40
@Eran It's a list of List<Instance> listInstances = new ArrayList<Instance>(); and the class Instance has vals = new ArrayList<Boolean>(); Let me know if it explains a bit
– user4046073
Mar 24 at 8:44
what's the type of
list
? perhaps you are using raw types.– Eran
Mar 24 at 8:40
what's the type of
list
? perhaps you are using raw types.– Eran
Mar 24 at 8:40
your list look like
List<List<Boolean>> list
– YCF_L
Mar 24 at 8:40
your list look like
List<List<Boolean>> list
– YCF_L
Mar 24 at 8:40
@Eran It's a list of List<Instance> listInstances = new ArrayList<Instance>(); and the class Instance has vals = new ArrayList<Boolean>(); Let me know if it explains a bit
– user4046073
Mar 24 at 8:44
@Eran It's a list of List<Instance> listInstances = new ArrayList<Instance>(); and the class Instance has vals = new ArrayList<Boolean>(); Let me know if it explains a bit
– user4046073
Mar 24 at 8:44
add a comment |
3 Answers
3
active
oldest
votes
... It's a list of
List<Instance> listInstances = new
and the class
ArrayList<Instance>();Instance
hasvals = new
....
ArrayList<Boolean>();
In this case your solution can look like :
public static Instance deleleNthElement(Instance instance, int index)
instance.getVals().remove(index - 1);
return instance;
then with stream you can call the method like so :
int index = 3;
listInstances = listInstances.stream()
.map(instance -> deleleNthElement(instance, index))
.collect(Collectors.toList());
I only want to remove the 3rd item. Would this keep the first 3?
– user4046073
Mar 24 at 8:46
@user4046073 I thought you want to delete the first three element, check my edit I think you need that solution instead the first
– YCF_L
Mar 24 at 8:52
Thanks. Which package do I need to import? I added import java.util.List; but that did not work.. it does not resolve subList and Collectors. I imported import java.util.*; too I am running Java java 11.0.2
– user4046073
Mar 24 at 9:00
1
@user4046073 yes there are, I was wrong, I think all you need isremove
, check my answer and gives me a feedback please
– YCF_L
Mar 24 at 9:26
1
@user4046073 please edit your question and put the Instance class and more code please to understand more, in your case you haveList<Instance> listInstances
and each instance haveList<Boolean> vals
, so to remove a val from each vals of each instanace you have to get the vals and remove the element from it, again show me more code so I can help you
– YCF_L
Mar 24 at 9:41
|
show 6 more comments
I see no error in your logic, I believe you are missing a ';' from the end of the remove(3).
By the way, List is an Interface, you will need to instanciate as an ArrayList (or some such).
Thanks. Yes sorry I have that ; in my code. But somehow that remove does not get resolved. I could remove a whole row but not the n th item in that row
– user4046073
Mar 24 at 9:28
add a comment |
I strung the following together, seems to do what you intended:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test
public static void main(String[] args) throws IOException
List<Boolean> row1 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,false,true,true));
List<Boolean> row2 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,true,false,true));
List<List<Boolean>> list = Arrays.asList(new ArrayList[] (ArrayList) row1, (ArrayList) row2);
for (int i=0;i<list.size();i++)
list.get(i).remove(3);// this remove method shows as an error in IDE
for (List<Boolean> ll : list)
for (Boolean l : ll)
System.out.print(l + ",");
System.out.println();
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55321983%2fjava-how-to-remove-item-from-nested-array-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
... It's a list of
List<Instance> listInstances = new
and the class
ArrayList<Instance>();Instance
hasvals = new
....
ArrayList<Boolean>();
In this case your solution can look like :
public static Instance deleleNthElement(Instance instance, int index)
instance.getVals().remove(index - 1);
return instance;
then with stream you can call the method like so :
int index = 3;
listInstances = listInstances.stream()
.map(instance -> deleleNthElement(instance, index))
.collect(Collectors.toList());
I only want to remove the 3rd item. Would this keep the first 3?
– user4046073
Mar 24 at 8:46
@user4046073 I thought you want to delete the first three element, check my edit I think you need that solution instead the first
– YCF_L
Mar 24 at 8:52
Thanks. Which package do I need to import? I added import java.util.List; but that did not work.. it does not resolve subList and Collectors. I imported import java.util.*; too I am running Java java 11.0.2
– user4046073
Mar 24 at 9:00
1
@user4046073 yes there are, I was wrong, I think all you need isremove
, check my answer and gives me a feedback please
– YCF_L
Mar 24 at 9:26
1
@user4046073 please edit your question and put the Instance class and more code please to understand more, in your case you haveList<Instance> listInstances
and each instance haveList<Boolean> vals
, so to remove a val from each vals of each instanace you have to get the vals and remove the element from it, again show me more code so I can help you
– YCF_L
Mar 24 at 9:41
|
show 6 more comments
... It's a list of
List<Instance> listInstances = new
and the class
ArrayList<Instance>();Instance
hasvals = new
....
ArrayList<Boolean>();
In this case your solution can look like :
public static Instance deleleNthElement(Instance instance, int index)
instance.getVals().remove(index - 1);
return instance;
then with stream you can call the method like so :
int index = 3;
listInstances = listInstances.stream()
.map(instance -> deleleNthElement(instance, index))
.collect(Collectors.toList());
I only want to remove the 3rd item. Would this keep the first 3?
– user4046073
Mar 24 at 8:46
@user4046073 I thought you want to delete the first three element, check my edit I think you need that solution instead the first
– YCF_L
Mar 24 at 8:52
Thanks. Which package do I need to import? I added import java.util.List; but that did not work.. it does not resolve subList and Collectors. I imported import java.util.*; too I am running Java java 11.0.2
– user4046073
Mar 24 at 9:00
1
@user4046073 yes there are, I was wrong, I think all you need isremove
, check my answer and gives me a feedback please
– YCF_L
Mar 24 at 9:26
1
@user4046073 please edit your question and put the Instance class and more code please to understand more, in your case you haveList<Instance> listInstances
and each instance haveList<Boolean> vals
, so to remove a val from each vals of each instanace you have to get the vals and remove the element from it, again show me more code so I can help you
– YCF_L
Mar 24 at 9:41
|
show 6 more comments
... It's a list of
List<Instance> listInstances = new
and the class
ArrayList<Instance>();Instance
hasvals = new
....
ArrayList<Boolean>();
In this case your solution can look like :
public static Instance deleleNthElement(Instance instance, int index)
instance.getVals().remove(index - 1);
return instance;
then with stream you can call the method like so :
int index = 3;
listInstances = listInstances.stream()
.map(instance -> deleleNthElement(instance, index))
.collect(Collectors.toList());
... It's a list of
List<Instance> listInstances = new
and the class
ArrayList<Instance>();Instance
hasvals = new
....
ArrayList<Boolean>();
In this case your solution can look like :
public static Instance deleleNthElement(Instance instance, int index)
instance.getVals().remove(index - 1);
return instance;
then with stream you can call the method like so :
int index = 3;
listInstances = listInstances.stream()
.map(instance -> deleleNthElement(instance, index))
.collect(Collectors.toList());
edited Mar 24 at 9:32
answered Mar 24 at 8:43
YCF_LYCF_L
34.8k104687
34.8k104687
I only want to remove the 3rd item. Would this keep the first 3?
– user4046073
Mar 24 at 8:46
@user4046073 I thought you want to delete the first three element, check my edit I think you need that solution instead the first
– YCF_L
Mar 24 at 8:52
Thanks. Which package do I need to import? I added import java.util.List; but that did not work.. it does not resolve subList and Collectors. I imported import java.util.*; too I am running Java java 11.0.2
– user4046073
Mar 24 at 9:00
1
@user4046073 yes there are, I was wrong, I think all you need isremove
, check my answer and gives me a feedback please
– YCF_L
Mar 24 at 9:26
1
@user4046073 please edit your question and put the Instance class and more code please to understand more, in your case you haveList<Instance> listInstances
and each instance haveList<Boolean> vals
, so to remove a val from each vals of each instanace you have to get the vals and remove the element from it, again show me more code so I can help you
– YCF_L
Mar 24 at 9:41
|
show 6 more comments
I only want to remove the 3rd item. Would this keep the first 3?
– user4046073
Mar 24 at 8:46
@user4046073 I thought you want to delete the first three element, check my edit I think you need that solution instead the first
– YCF_L
Mar 24 at 8:52
Thanks. Which package do I need to import? I added import java.util.List; but that did not work.. it does not resolve subList and Collectors. I imported import java.util.*; too I am running Java java 11.0.2
– user4046073
Mar 24 at 9:00
1
@user4046073 yes there are, I was wrong, I think all you need isremove
, check my answer and gives me a feedback please
– YCF_L
Mar 24 at 9:26
1
@user4046073 please edit your question and put the Instance class and more code please to understand more, in your case you haveList<Instance> listInstances
and each instance haveList<Boolean> vals
, so to remove a val from each vals of each instanace you have to get the vals and remove the element from it, again show me more code so I can help you
– YCF_L
Mar 24 at 9:41
I only want to remove the 3rd item. Would this keep the first 3?
– user4046073
Mar 24 at 8:46
I only want to remove the 3rd item. Would this keep the first 3?
– user4046073
Mar 24 at 8:46
@user4046073 I thought you want to delete the first three element, check my edit I think you need that solution instead the first
– YCF_L
Mar 24 at 8:52
@user4046073 I thought you want to delete the first three element, check my edit I think you need that solution instead the first
– YCF_L
Mar 24 at 8:52
Thanks. Which package do I need to import? I added import java.util.List; but that did not work.. it does not resolve subList and Collectors. I imported import java.util.*; too I am running Java java 11.0.2
– user4046073
Mar 24 at 9:00
Thanks. Which package do I need to import? I added import java.util.List; but that did not work.. it does not resolve subList and Collectors. I imported import java.util.*; too I am running Java java 11.0.2
– user4046073
Mar 24 at 9:00
1
1
@user4046073 yes there are, I was wrong, I think all you need is
remove
, check my answer and gives me a feedback please– YCF_L
Mar 24 at 9:26
@user4046073 yes there are, I was wrong, I think all you need is
remove
, check my answer and gives me a feedback please– YCF_L
Mar 24 at 9:26
1
1
@user4046073 please edit your question and put the Instance class and more code please to understand more, in your case you have
List<Instance> listInstances
and each instance have List<Boolean> vals
, so to remove a val from each vals of each instanace you have to get the vals and remove the element from it, again show me more code so I can help you– YCF_L
Mar 24 at 9:41
@user4046073 please edit your question and put the Instance class and more code please to understand more, in your case you have
List<Instance> listInstances
and each instance have List<Boolean> vals
, so to remove a val from each vals of each instanace you have to get the vals and remove the element from it, again show me more code so I can help you– YCF_L
Mar 24 at 9:41
|
show 6 more comments
I see no error in your logic, I believe you are missing a ';' from the end of the remove(3).
By the way, List is an Interface, you will need to instanciate as an ArrayList (or some such).
Thanks. Yes sorry I have that ; in my code. But somehow that remove does not get resolved. I could remove a whole row but not the n th item in that row
– user4046073
Mar 24 at 9:28
add a comment |
I see no error in your logic, I believe you are missing a ';' from the end of the remove(3).
By the way, List is an Interface, you will need to instanciate as an ArrayList (or some such).
Thanks. Yes sorry I have that ; in my code. But somehow that remove does not get resolved. I could remove a whole row but not the n th item in that row
– user4046073
Mar 24 at 9:28
add a comment |
I see no error in your logic, I believe you are missing a ';' from the end of the remove(3).
By the way, List is an Interface, you will need to instanciate as an ArrayList (or some such).
I see no error in your logic, I believe you are missing a ';' from the end of the remove(3).
By the way, List is an Interface, you will need to instanciate as an ArrayList (or some such).
answered Mar 24 at 9:24
Bill NaylorBill Naylor
7117
7117
Thanks. Yes sorry I have that ; in my code. But somehow that remove does not get resolved. I could remove a whole row but not the n th item in that row
– user4046073
Mar 24 at 9:28
add a comment |
Thanks. Yes sorry I have that ; in my code. But somehow that remove does not get resolved. I could remove a whole row but not the n th item in that row
– user4046073
Mar 24 at 9:28
Thanks. Yes sorry I have that ; in my code. But somehow that remove does not get resolved. I could remove a whole row but not the n th item in that row
– user4046073
Mar 24 at 9:28
Thanks. Yes sorry I have that ; in my code. But somehow that remove does not get resolved. I could remove a whole row but not the n th item in that row
– user4046073
Mar 24 at 9:28
add a comment |
I strung the following together, seems to do what you intended:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test
public static void main(String[] args) throws IOException
List<Boolean> row1 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,false,true,true));
List<Boolean> row2 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,true,false,true));
List<List<Boolean>> list = Arrays.asList(new ArrayList[] (ArrayList) row1, (ArrayList) row2);
for (int i=0;i<list.size();i++)
list.get(i).remove(3);// this remove method shows as an error in IDE
for (List<Boolean> ll : list)
for (Boolean l : ll)
System.out.print(l + ",");
System.out.println();
add a comment |
I strung the following together, seems to do what you intended:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test
public static void main(String[] args) throws IOException
List<Boolean> row1 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,false,true,true));
List<Boolean> row2 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,true,false,true));
List<List<Boolean>> list = Arrays.asList(new ArrayList[] (ArrayList) row1, (ArrayList) row2);
for (int i=0;i<list.size();i++)
list.get(i).remove(3);// this remove method shows as an error in IDE
for (List<Boolean> ll : list)
for (Boolean l : ll)
System.out.print(l + ",");
System.out.println();
add a comment |
I strung the following together, seems to do what you intended:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test
public static void main(String[] args) throws IOException
List<Boolean> row1 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,false,true,true));
List<Boolean> row2 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,true,false,true));
List<List<Boolean>> list = Arrays.asList(new ArrayList[] (ArrayList) row1, (ArrayList) row2);
for (int i=0;i<list.size();i++)
list.get(i).remove(3);// this remove method shows as an error in IDE
for (List<Boolean> ll : list)
for (Boolean l : ll)
System.out.print(l + ",");
System.out.println();
I strung the following together, seems to do what you intended:
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test
public static void main(String[] args) throws IOException
List<Boolean> row1 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,false,true,true));
List<Boolean> row2 = new ArrayList<Boolean>(Arrays.asList(new Boolean[] true,true,false,true));
List<List<Boolean>> list = Arrays.asList(new ArrayList[] (ArrayList) row1, (ArrayList) row2);
for (int i=0;i<list.size();i++)
list.get(i).remove(3);// this remove method shows as an error in IDE
for (List<Boolean> ll : list)
for (Boolean l : ll)
System.out.print(l + ",");
System.out.println();
answered Mar 24 at 10:07
Bill NaylorBill Naylor
7117
7117
add a comment |
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.
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%2f55321983%2fjava-how-to-remove-item-from-nested-array-list%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
what's the type of
list
? perhaps you are using raw types.– Eran
Mar 24 at 8:40
your list look like
List<List<Boolean>> list
– YCF_L
Mar 24 at 8:40
@Eran It's a list of List<Instance> listInstances = new ArrayList<Instance>(); and the class Instance has vals = new ArrayList<Boolean>(); Let me know if it explains a bit
– user4046073
Mar 24 at 8:44