(Java) add list of lists to ArrayList<List>
I have a list of integer lists that I have initialized.
ArrayList<List<Integer>> list = new ArrayList();
I also have some data that I need to append to this list.
list = [[1,5,9], [3,5,7], [1,4,7],[2,5,8],[3,6,9], [1,2,3], [4,5,6],[7,8,9]];
EDIT:
I understand that this is not a legal way to express a list of lists in java (python background) but I do not know how to do so in Java.
What would be the best way to append this data to a list of lists in Java?
java list arraylist
add a comment |
I have a list of integer lists that I have initialized.
ArrayList<List<Integer>> list = new ArrayList();
I also have some data that I need to append to this list.
list = [[1,5,9], [3,5,7], [1,4,7],[2,5,8],[3,6,9], [1,2,3], [4,5,6],[7,8,9]];
EDIT:
I understand that this is not a legal way to express a list of lists in java (python background) but I do not know how to do so in Java.
What would be the best way to append this data to a list of lists in Java?
java list arraylist
How is that data represented?
– Mureinik
Oct 19 '18 at 19:00
1
right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java
– entercaspa
Oct 19 '18 at 19:01
add a comment |
I have a list of integer lists that I have initialized.
ArrayList<List<Integer>> list = new ArrayList();
I also have some data that I need to append to this list.
list = [[1,5,9], [3,5,7], [1,4,7],[2,5,8],[3,6,9], [1,2,3], [4,5,6],[7,8,9]];
EDIT:
I understand that this is not a legal way to express a list of lists in java (python background) but I do not know how to do so in Java.
What would be the best way to append this data to a list of lists in Java?
java list arraylist
I have a list of integer lists that I have initialized.
ArrayList<List<Integer>> list = new ArrayList();
I also have some data that I need to append to this list.
list = [[1,5,9], [3,5,7], [1,4,7],[2,5,8],[3,6,9], [1,2,3], [4,5,6],[7,8,9]];
EDIT:
I understand that this is not a legal way to express a list of lists in java (python background) but I do not know how to do so in Java.
What would be the best way to append this data to a list of lists in Java?
java list arraylist
java list arraylist
edited Oct 19 '18 at 19:22
Mureinik
182k22133200
182k22133200
asked Oct 19 '18 at 18:58
entercaspaentercaspa
335114
335114
How is that data represented?
– Mureinik
Oct 19 '18 at 19:00
1
right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java
– entercaspa
Oct 19 '18 at 19:01
add a comment |
How is that data represented?
– Mureinik
Oct 19 '18 at 19:00
1
right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java
– entercaspa
Oct 19 '18 at 19:01
How is that data represented?
– Mureinik
Oct 19 '18 at 19:00
How is that data represented?
– Mureinik
Oct 19 '18 at 19:00
1
1
right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java
– entercaspa
Oct 19 '18 at 19:01
right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java
– entercaspa
Oct 19 '18 at 19:01
add a comment |
3 Answers
3
active
oldest
votes
You could do it like this
List<List<Integer>> lists = Arrays.asList(
Arrays.asList(1,5,9),
Arrays.asList(3,5,7),
Arrays.asList(1,4,7),
Arrays.asList(2,5,8),
Arrays.asList(3,6,9),
Arrays.asList(1,2,3),
Arrays.asList(4,5,6),
Arrays.asList(7,8,9)
);
if it is okay that the lists can’t change their size once they’ve been created.
add a comment |
The easiest approach would probably be to use Arrays#asList
:
List<List<Integer>> list =
Arrays.asList(Arrays.asList(1, 5, 9),
Arrays.asList(3, 5, 7),
/* etc... */
);
EDIT:
If you're using Java 9 or above, you could also use the slightly more elegant List#of
:
List<List<Integer>> list =
List.of(List.of(1, 5, 9),
List.of(3, 5, 7),
/* etc... */
);
add a comment |
Using java-8 :
List<List<Integer>> lists = Stream
.of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
.collect(Collectors.toList());
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%2f52898430%2fjava-add-list-of-lists-to-arraylistlistinteger%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
You could do it like this
List<List<Integer>> lists = Arrays.asList(
Arrays.asList(1,5,9),
Arrays.asList(3,5,7),
Arrays.asList(1,4,7),
Arrays.asList(2,5,8),
Arrays.asList(3,6,9),
Arrays.asList(1,2,3),
Arrays.asList(4,5,6),
Arrays.asList(7,8,9)
);
if it is okay that the lists can’t change their size once they’ve been created.
add a comment |
You could do it like this
List<List<Integer>> lists = Arrays.asList(
Arrays.asList(1,5,9),
Arrays.asList(3,5,7),
Arrays.asList(1,4,7),
Arrays.asList(2,5,8),
Arrays.asList(3,6,9),
Arrays.asList(1,2,3),
Arrays.asList(4,5,6),
Arrays.asList(7,8,9)
);
if it is okay that the lists can’t change their size once they’ve been created.
add a comment |
You could do it like this
List<List<Integer>> lists = Arrays.asList(
Arrays.asList(1,5,9),
Arrays.asList(3,5,7),
Arrays.asList(1,4,7),
Arrays.asList(2,5,8),
Arrays.asList(3,6,9),
Arrays.asList(1,2,3),
Arrays.asList(4,5,6),
Arrays.asList(7,8,9)
);
if it is okay that the lists can’t change their size once they’ve been created.
You could do it like this
List<List<Integer>> lists = Arrays.asList(
Arrays.asList(1,5,9),
Arrays.asList(3,5,7),
Arrays.asList(1,4,7),
Arrays.asList(2,5,8),
Arrays.asList(3,6,9),
Arrays.asList(1,2,3),
Arrays.asList(4,5,6),
Arrays.asList(7,8,9)
);
if it is okay that the lists can’t change their size once they’ve been created.
edited Nov 25 '18 at 16:42
answered Oct 19 '18 at 19:03
AntimonAntimon
1197
1197
add a comment |
add a comment |
The easiest approach would probably be to use Arrays#asList
:
List<List<Integer>> list =
Arrays.asList(Arrays.asList(1, 5, 9),
Arrays.asList(3, 5, 7),
/* etc... */
);
EDIT:
If you're using Java 9 or above, you could also use the slightly more elegant List#of
:
List<List<Integer>> list =
List.of(List.of(1, 5, 9),
List.of(3, 5, 7),
/* etc... */
);
add a comment |
The easiest approach would probably be to use Arrays#asList
:
List<List<Integer>> list =
Arrays.asList(Arrays.asList(1, 5, 9),
Arrays.asList(3, 5, 7),
/* etc... */
);
EDIT:
If you're using Java 9 or above, you could also use the slightly more elegant List#of
:
List<List<Integer>> list =
List.of(List.of(1, 5, 9),
List.of(3, 5, 7),
/* etc... */
);
add a comment |
The easiest approach would probably be to use Arrays#asList
:
List<List<Integer>> list =
Arrays.asList(Arrays.asList(1, 5, 9),
Arrays.asList(3, 5, 7),
/* etc... */
);
EDIT:
If you're using Java 9 or above, you could also use the slightly more elegant List#of
:
List<List<Integer>> list =
List.of(List.of(1, 5, 9),
List.of(3, 5, 7),
/* etc... */
);
The easiest approach would probably be to use Arrays#asList
:
List<List<Integer>> list =
Arrays.asList(Arrays.asList(1, 5, 9),
Arrays.asList(3, 5, 7),
/* etc... */
);
EDIT:
If you're using Java 9 or above, you could also use the slightly more elegant List#of
:
List<List<Integer>> list =
List.of(List.of(1, 5, 9),
List.of(3, 5, 7),
/* etc... */
);
edited Oct 19 '18 at 19:10
answered Oct 19 '18 at 19:04
MureinikMureinik
182k22133200
182k22133200
add a comment |
add a comment |
Using java-8 :
List<List<Integer>> lists = Stream
.of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
.collect(Collectors.toList());
add a comment |
Using java-8 :
List<List<Integer>> lists = Stream
.of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
.collect(Collectors.toList());
add a comment |
Using java-8 :
List<List<Integer>> lists = Stream
.of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
.collect(Collectors.toList());
Using java-8 :
List<List<Integer>> lists = Stream
.of(Arrays.asList(1, 5, 9), Arrays.asList(3, 5, 7), Arrays.asList(1, 4, 7),
Arrays.asList(2, 5, 8), Arrays.asList(3, 6, 9), Arrays.asList(1, 2, 3),
Arrays.asList(4, 5, 6), Arrays.asList(7, 8, 9))
.collect(Collectors.toList());
edited Oct 21 '18 at 6:37
answered Oct 20 '18 at 6:05
Nicholas KNicholas K
6,76761133
6,76761133
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%2f52898430%2fjava-add-list-of-lists-to-arraylistlistinteger%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
How is that data represented?
– Mureinik
Oct 19 '18 at 19:00
1
right now just like the list variable above, I come from a python background so the list of lists is there, i dont know how to express it in java
– entercaspa
Oct 19 '18 at 19:01