Auto-generate variable size 2D array from 1D arrays
I am looking for some advice on how I can create a method that generates a 2D array based two parameters, an integer s that will dictate the number of rows of the 2D array and int x would be where different 1D arrays of length 20 will fill each row of the 2D array. So far this the the method that I came up with but it only fills each row with 0s only one row is filled with the input array. I basically need a to fill each row of a autogenerated 2D array with a bunch of same sized 1D arrays. please help. thx !
public class c {
public int f;
public int a(int x, int s){
f = new int [s][20];
for(int j = 0; j < x.length; j++) {
f[s-1][j] = x[j];
}
return f;
}
public void d(){
for (int i =0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
System.out.print(f[i][j] + " ");
}
System.out.println(" ");
}
}
}
java arrays multidimensional-array methods
add a comment |
I am looking for some advice on how I can create a method that generates a 2D array based two parameters, an integer s that will dictate the number of rows of the 2D array and int x would be where different 1D arrays of length 20 will fill each row of the 2D array. So far this the the method that I came up with but it only fills each row with 0s only one row is filled with the input array. I basically need a to fill each row of a autogenerated 2D array with a bunch of same sized 1D arrays. please help. thx !
public class c {
public int f;
public int a(int x, int s){
f = new int [s][20];
for(int j = 0; j < x.length; j++) {
f[s-1][j] = x[j];
}
return f;
}
public void d(){
for (int i =0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
System.out.print(f[i][j] + " ");
}
System.out.println(" ");
}
}
}
java arrays multidimensional-array methods
You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
– Hrudayanath
Nov 12 '18 at 7:07
Possible duplicate of How to convert a 1d array to 2d array?
– LuCio
Nov 12 '18 at 8:42
add a comment |
I am looking for some advice on how I can create a method that generates a 2D array based two parameters, an integer s that will dictate the number of rows of the 2D array and int x would be where different 1D arrays of length 20 will fill each row of the 2D array. So far this the the method that I came up with but it only fills each row with 0s only one row is filled with the input array. I basically need a to fill each row of a autogenerated 2D array with a bunch of same sized 1D arrays. please help. thx !
public class c {
public int f;
public int a(int x, int s){
f = new int [s][20];
for(int j = 0; j < x.length; j++) {
f[s-1][j] = x[j];
}
return f;
}
public void d(){
for (int i =0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
System.out.print(f[i][j] + " ");
}
System.out.println(" ");
}
}
}
java arrays multidimensional-array methods
I am looking for some advice on how I can create a method that generates a 2D array based two parameters, an integer s that will dictate the number of rows of the 2D array and int x would be where different 1D arrays of length 20 will fill each row of the 2D array. So far this the the method that I came up with but it only fills each row with 0s only one row is filled with the input array. I basically need a to fill each row of a autogenerated 2D array with a bunch of same sized 1D arrays. please help. thx !
public class c {
public int f;
public int a(int x, int s){
f = new int [s][20];
for(int j = 0; j < x.length; j++) {
f[s-1][j] = x[j];
}
return f;
}
public void d(){
for (int i =0; i < f.length; i++) {
for(int j = 0; j < f[i].length; j++) {
System.out.print(f[i][j] + " ");
}
System.out.println(" ");
}
}
}
java arrays multidimensional-array methods
java arrays multidimensional-array methods
edited Nov 12 '18 at 7:18
Deedar Ali Brohi
21519
21519
asked Nov 12 '18 at 6:57
Sam D
1
1
You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
– Hrudayanath
Nov 12 '18 at 7:07
Possible duplicate of How to convert a 1d array to 2d array?
– LuCio
Nov 12 '18 at 8:42
add a comment |
You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
– Hrudayanath
Nov 12 '18 at 7:07
Possible duplicate of How to convert a 1d array to 2d array?
– LuCio
Nov 12 '18 at 8:42
You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
– Hrudayanath
Nov 12 '18 at 7:07
You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
– Hrudayanath
Nov 12 '18 at 7:07
Possible duplicate of How to convert a 1d array to 2d array?
– LuCio
Nov 12 '18 at 8:42
Possible duplicate of How to convert a 1d array to 2d array?
– LuCio
Nov 12 '18 at 8:42
add a comment |
2 Answers
2
active
oldest
votes
This can be achieved using stream api in java
int arrInt = { { 1, 2 }, { 3, 4, 5 } };
int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
System.out.println(Arrays.toString(result));
add a comment |
I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:
- Create new 2D array
- Iterate over each row and put given array into it
Example:
public static int createArray(int arr, int totalRows) {
int res = new int[totalRows][arr.length];
for (int row = 0; row < totalRows; row++)
System.arraycopy(arr, 0, res[row], 0, res[row].length);
return res;
}
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
Nov 12 '18 at 8:29
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%2f53257208%2fauto-generate-variable-size-2d-array-from-1d-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
This can be achieved using stream api in java
int arrInt = { { 1, 2 }, { 3, 4, 5 } };
int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
System.out.println(Arrays.toString(result));
add a comment |
This can be achieved using stream api in java
int arrInt = { { 1, 2 }, { 3, 4, 5 } };
int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
System.out.println(Arrays.toString(result));
add a comment |
This can be achieved using stream api in java
int arrInt = { { 1, 2 }, { 3, 4, 5 } };
int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
System.out.println(Arrays.toString(result));
This can be achieved using stream api in java
int arrInt = { { 1, 2 }, { 3, 4, 5 } };
int result = Arrays.stream(arrInt).flatMapToInt(Arrays::stream).toArray();
System.out.println(Arrays.toString(result));
edited Nov 12 '18 at 8:38
answered Nov 12 '18 at 8:30
Navin Gelot
2531216
2531216
add a comment |
add a comment |
I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:
- Create new 2D array
- Iterate over each row and put given array into it
Example:
public static int createArray(int arr, int totalRows) {
int res = new int[totalRows][arr.length];
for (int row = 0; row < totalRows; row++)
System.arraycopy(arr, 0, res[row], 0, res[row].length);
return res;
}
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
Nov 12 '18 at 8:29
add a comment |
I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:
- Create new 2D array
- Iterate over each row and put given array into it
Example:
public static int createArray(int arr, int totalRows) {
int res = new int[totalRows][arr.length];
for (int row = 0; row < totalRows; row++)
System.arraycopy(arr, 0, res[row], 0, res[row].length);
return res;
}
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
Nov 12 '18 at 8:29
add a comment |
I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:
- Create new 2D array
- Iterate over each row and put given array into it
Example:
public static int createArray(int arr, int totalRows) {
int res = new int[totalRows][arr.length];
for (int row = 0; row < totalRows; row++)
System.arraycopy(arr, 0, res[row], 0, res[row].length);
return res;
}
I can see, that all you need to do is just create 2D array with given number of rows and copy given 1D array to the to the each row. This is pretty simple:
- Create new 2D array
- Iterate over each row and put given array into it
Example:
public static int createArray(int arr, int totalRows) {
int res = new int[totalRows][arr.length];
for (int row = 0; row < totalRows; row++)
System.arraycopy(arr, 0, res[row], 0, res[row].length);
return res;
}
edited Nov 12 '18 at 8:42
answered Nov 12 '18 at 7:23
oleg.cherednik
5,87821017
5,87821017
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
Nov 12 '18 at 8:29
add a comment |
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
Nov 12 '18 at 8:29
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
Nov 12 '18 at 8:29
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written
– WhatsThePoint
Nov 12 '18 at 8:29
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%2f53257208%2fauto-generate-variable-size-2d-array-from-1d-arrays%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
You are initializing only one row f[s-1][j] = x[j]; and each time a() function is called you are creating new 2D array and initializing only one row in it.
– Hrudayanath
Nov 12 '18 at 7:07
Possible duplicate of How to convert a 1d array to 2d array?
– LuCio
Nov 12 '18 at 8:42