How to dynamically change a list value to another list in Java or Groovy
For Example, I have a list like below,
list1 = ["a","b","c"] now i want to add value to the list[1] index and that index
will be another list like,
list2 = ["w","x","y"]
The output will be ["a",["b","w","x","y"],"c"] I don't want to replace 1 index
Is it possible?
java groovy
add a comment |
For Example, I have a list like below,
list1 = ["a","b","c"] now i want to add value to the list[1] index and that index
will be another list like,
list2 = ["w","x","y"]
The output will be ["a",["b","w","x","y"],"c"] I don't want to replace 1 index
Is it possible?
java groovy
2
What aboutlist1.set(1, ["b","x","y"])
?
– ernest_k
Nov 21 '18 at 7:05
1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
– masud bappy
Nov 21 '18 at 7:10
or even like this:list1[1] = ["b","x","y"]
– daggett
Nov 21 '18 at 8:02
add a comment |
For Example, I have a list like below,
list1 = ["a","b","c"] now i want to add value to the list[1] index and that index
will be another list like,
list2 = ["w","x","y"]
The output will be ["a",["b","w","x","y"],"c"] I don't want to replace 1 index
Is it possible?
java groovy
For Example, I have a list like below,
list1 = ["a","b","c"] now i want to add value to the list[1] index and that index
will be another list like,
list2 = ["w","x","y"]
The output will be ["a",["b","w","x","y"],"c"] I don't want to replace 1 index
Is it possible?
java groovy
java groovy
edited Nov 21 '18 at 9:50
masud bappy
asked Nov 21 '18 at 7:03
masud bappymasud bappy
33
33
2
What aboutlist1.set(1, ["b","x","y"])
?
– ernest_k
Nov 21 '18 at 7:05
1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
– masud bappy
Nov 21 '18 at 7:10
or even like this:list1[1] = ["b","x","y"]
– daggett
Nov 21 '18 at 8:02
add a comment |
2
What aboutlist1.set(1, ["b","x","y"])
?
– ernest_k
Nov 21 '18 at 7:05
1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
– masud bappy
Nov 21 '18 at 7:10
or even like this:list1[1] = ["b","x","y"]
– daggett
Nov 21 '18 at 8:02
2
2
What about
list1.set(1, ["b","x","y"])
?– ernest_k
Nov 21 '18 at 7:05
What about
list1.set(1, ["b","x","y"])
?– ernest_k
Nov 21 '18 at 7:05
1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
– masud bappy
Nov 21 '18 at 7:10
1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
– masud bappy
Nov 21 '18 at 7:10
or even like this:
list1[1] = ["b","x","y"]
– daggett
Nov 21 '18 at 8:02
or even like this:
list1[1] = ["b","x","y"]
– daggett
Nov 21 '18 at 8:02
add a comment |
4 Answers
4
active
oldest
votes
Your base list contains two types: String
and List<String>
You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.
You could use a helper method like :
private static List mergeIntoList(List source, List listToAdd, int index) {
List innerList = new ArrayList(listToAdd);
innerList.add(0, source.get(index));
List mergedList = new ArrayList(source);
mergedList.set(index, innerList);
return mergedList;
}
Exemple:
List source = Arrays.asList("a", "b", "c");
List listToAdd = Arrays.asList("w", "x", "y");
System.out.println(mergeIntoList(source, listToAdd, 1));
output: [a, [b, w, x, y], c]
But I repeat, you should avoid raw types, so this solution is not recommended
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:02
@masudbappy i updated my answer because you change your question
– Jesus Zavarce
Nov 21 '18 at 12:08
add a comment |
It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
When retrieving the Objects you would need to check what type they are with instanceof and then cast them.
Actually i am using groovy and so far groovy is type safe.
– masud bappy
Nov 21 '18 at 7:22
add a comment |
In java it is not possible taking type of list1 as String
.
Type of list1 seems String and you are adding another list into list1 which is not correct.
But it is possible taking type list1 as Object
List<Object> list1=new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
List<Object> innerlist=new ArrayList<>(); //Inner List
list1.add(innerlist);
However it is not recommended way.
add a comment |
You can add value to a specific index as below
List list = new ArrayList();
List l1 = new ArrayList();
l1.add("3");
l1.add("4");
list.add("1");
list.add("5");
list.add(1, l1);
Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException
Please try below logic
public List replaceIndex(List original, List replace, int index) {
Object object = original.remove(index);
replace.add(0, object);
original.add(index, replace);
return original;
}
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:05
@masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
– darshakat
Nov 21 '18 at 8:33
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%2f53406827%2fhow-to-dynamically-change-a-list-value-to-another-list-in-java-or-groovy%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your base list contains two types: String
and List<String>
You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.
You could use a helper method like :
private static List mergeIntoList(List source, List listToAdd, int index) {
List innerList = new ArrayList(listToAdd);
innerList.add(0, source.get(index));
List mergedList = new ArrayList(source);
mergedList.set(index, innerList);
return mergedList;
}
Exemple:
List source = Arrays.asList("a", "b", "c");
List listToAdd = Arrays.asList("w", "x", "y");
System.out.println(mergeIntoList(source, listToAdd, 1));
output: [a, [b, w, x, y], c]
But I repeat, you should avoid raw types, so this solution is not recommended
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:02
@masudbappy i updated my answer because you change your question
– Jesus Zavarce
Nov 21 '18 at 12:08
add a comment |
Your base list contains two types: String
and List<String>
You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.
You could use a helper method like :
private static List mergeIntoList(List source, List listToAdd, int index) {
List innerList = new ArrayList(listToAdd);
innerList.add(0, source.get(index));
List mergedList = new ArrayList(source);
mergedList.set(index, innerList);
return mergedList;
}
Exemple:
List source = Arrays.asList("a", "b", "c");
List listToAdd = Arrays.asList("w", "x", "y");
System.out.println(mergeIntoList(source, listToAdd, 1));
output: [a, [b, w, x, y], c]
But I repeat, you should avoid raw types, so this solution is not recommended
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:02
@masudbappy i updated my answer because you change your question
– Jesus Zavarce
Nov 21 '18 at 12:08
add a comment |
Your base list contains two types: String
and List<String>
You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.
You could use a helper method like :
private static List mergeIntoList(List source, List listToAdd, int index) {
List innerList = new ArrayList(listToAdd);
innerList.add(0, source.get(index));
List mergedList = new ArrayList(source);
mergedList.set(index, innerList);
return mergedList;
}
Exemple:
List source = Arrays.asList("a", "b", "c");
List listToAdd = Arrays.asList("w", "x", "y");
System.out.println(mergeIntoList(source, listToAdd, 1));
output: [a, [b, w, x, y], c]
But I repeat, you should avoid raw types, so this solution is not recommended
Your base list contains two types: String
and List<String>
You can create a list as your base list in java but is not a good idea because you are mixing types. That is a bad practice. You should avoid raw types.
You could use a helper method like :
private static List mergeIntoList(List source, List listToAdd, int index) {
List innerList = new ArrayList(listToAdd);
innerList.add(0, source.get(index));
List mergedList = new ArrayList(source);
mergedList.set(index, innerList);
return mergedList;
}
Exemple:
List source = Arrays.asList("a", "b", "c");
List listToAdd = Arrays.asList("w", "x", "y");
System.out.println(mergeIntoList(source, listToAdd, 1));
output: [a, [b, w, x, y], c]
But I repeat, you should avoid raw types, so this solution is not recommended
edited Nov 21 '18 at 12:07
answered Nov 21 '18 at 7:44
Jesus ZavarceJesus Zavarce
1,049818
1,049818
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:02
@masudbappy i updated my answer because you change your question
– Jesus Zavarce
Nov 21 '18 at 12:08
add a comment |
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:02
@masudbappy i updated my answer because you change your question
– Jesus Zavarce
Nov 21 '18 at 12:08
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:02
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:02
@masudbappy i updated my answer because you change your question
– Jesus Zavarce
Nov 21 '18 at 12:08
@masudbappy i updated my answer because you change your question
– Jesus Zavarce
Nov 21 '18 at 12:08
add a comment |
It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
When retrieving the Objects you would need to check what type they are with instanceof and then cast them.
Actually i am using groovy and so far groovy is type safe.
– masud bappy
Nov 21 '18 at 7:22
add a comment |
It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
When retrieving the Objects you would need to check what type they are with instanceof and then cast them.
Actually i am using groovy and so far groovy is type safe.
– masud bappy
Nov 21 '18 at 7:22
add a comment |
It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
When retrieving the Objects you would need to check what type they are with instanceof and then cast them.
It's possible, but not very type safe (=don't do it). You could use a List and then add your values, which could be any type of Object (Integer String... or another List).
When retrieving the Objects you would need to check what type they are with instanceof and then cast them.
answered Nov 21 '18 at 7:18
Andreas HartmannAndreas Hartmann
7791024
7791024
Actually i am using groovy and so far groovy is type safe.
– masud bappy
Nov 21 '18 at 7:22
add a comment |
Actually i am using groovy and so far groovy is type safe.
– masud bappy
Nov 21 '18 at 7:22
Actually i am using groovy and so far groovy is type safe.
– masud bappy
Nov 21 '18 at 7:22
Actually i am using groovy and so far groovy is type safe.
– masud bappy
Nov 21 '18 at 7:22
add a comment |
In java it is not possible taking type of list1 as String
.
Type of list1 seems String and you are adding another list into list1 which is not correct.
But it is possible taking type list1 as Object
List<Object> list1=new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
List<Object> innerlist=new ArrayList<>(); //Inner List
list1.add(innerlist);
However it is not recommended way.
add a comment |
In java it is not possible taking type of list1 as String
.
Type of list1 seems String and you are adding another list into list1 which is not correct.
But it is possible taking type list1 as Object
List<Object> list1=new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
List<Object> innerlist=new ArrayList<>(); //Inner List
list1.add(innerlist);
However it is not recommended way.
add a comment |
In java it is not possible taking type of list1 as String
.
Type of list1 seems String and you are adding another list into list1 which is not correct.
But it is possible taking type list1 as Object
List<Object> list1=new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
List<Object> innerlist=new ArrayList<>(); //Inner List
list1.add(innerlist);
However it is not recommended way.
In java it is not possible taking type of list1 as String
.
Type of list1 seems String and you are adding another list into list1 which is not correct.
But it is possible taking type list1 as Object
List<Object> list1=new ArrayList<>();
list1.add("a");
list1.add("b");
list1.add("c");
List<Object> innerlist=new ArrayList<>(); //Inner List
list1.add(innerlist);
However it is not recommended way.
answered Nov 21 '18 at 7:23
TarunTarun
649414
649414
add a comment |
add a comment |
You can add value to a specific index as below
List list = new ArrayList();
List l1 = new ArrayList();
l1.add("3");
l1.add("4");
list.add("1");
list.add("5");
list.add(1, l1);
Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException
Please try below logic
public List replaceIndex(List original, List replace, int index) {
Object object = original.remove(index);
replace.add(0, object);
original.add(index, replace);
return original;
}
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:05
@masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
– darshakat
Nov 21 '18 at 8:33
add a comment |
You can add value to a specific index as below
List list = new ArrayList();
List l1 = new ArrayList();
l1.add("3");
l1.add("4");
list.add("1");
list.add("5");
list.add(1, l1);
Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException
Please try below logic
public List replaceIndex(List original, List replace, int index) {
Object object = original.remove(index);
replace.add(0, object);
original.add(index, replace);
return original;
}
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:05
@masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
– darshakat
Nov 21 '18 at 8:33
add a comment |
You can add value to a specific index as below
List list = new ArrayList();
List l1 = new ArrayList();
l1.add("3");
l1.add("4");
list.add("1");
list.add("5");
list.add(1, l1);
Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException
Please try below logic
public List replaceIndex(List original, List replace, int index) {
Object object = original.remove(index);
replace.add(0, object);
original.add(index, replace);
return original;
}
You can add value to a specific index as below
List list = new ArrayList();
List l1 = new ArrayList();
l1.add("3");
l1.add("4");
list.add("1");
list.add("5");
list.add(1, l1);
Please make sure that index in the list size, otherwise will get the IndexOutOfBoundsException
Please try below logic
public List replaceIndex(List original, List replace, int index) {
Object object = original.remove(index);
replace.add(0, object);
original.add(index, replace);
return original;
}
edited Nov 21 '18 at 8:33
answered Nov 21 '18 at 7:32
darshakatdarshakat
366110
366110
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:05
@masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
– darshakat
Nov 21 '18 at 8:33
add a comment |
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:05
@masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
– darshakat
Nov 21 '18 at 8:33
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:05
def newList = newList.add("D") def list1 = ["a","B","C"] list1.set(1,newList) println(list1) // Actually when i set a new list at 1th index it is replacing the 1th index value but i want that value also. And it is actually groovy problem
– masud bappy
Nov 21 '18 at 8:05
@masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
– darshakat
Nov 21 '18 at 8:33
@masudbappy Seems you have to write a small logic to fulfill your requirement. I'll update the answer.
– darshakat
Nov 21 '18 at 8:33
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%2f53406827%2fhow-to-dynamically-change-a-list-value-to-another-list-in-java-or-groovy%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
2
What about
list1.set(1, ["b","x","y"])
?– ernest_k
Nov 21 '18 at 7:05
1 index will be change at run time, like when i will run the program another element will insert that index automatically. And also i don't know the value of 1th index
– masud bappy
Nov 21 '18 at 7:10
or even like this:
list1[1] = ["b","x","y"]
– daggett
Nov 21 '18 at 8:02