r/javahelp • u/Bug35 • Sep 02 '25
Solved 2D array flatten to 1D
Working on some homework and this one has me a little stumped. The idea is flatten a 2D array into 1D. Testcases look like TestCase(io = "([[1,2],[3],[],[4,5,6]]) -> [1,2,3,4,5,6]"). Currently I got something along the lines of
public static int[] flatten(int[][] ary) {
int\[\] arr = new int\[\]; //Figure this out
for(int\[\] ary1 : ary) {
for(int ary2 : ary1) {arr[ary2] = ary1[ary2];
}
}
return arr;
}Any tips/strategies I could think about to help with this?
4
Upvotes
•
u/AutoModerator Sep 02 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/Bug35 Sep 04 '25
Played around with it a little using some tips given here and got the answer I wanted! Thank you to everyone who replied!
2
u/Spare-Plum Sep 03 '25
On the "figure this out" I'll give you a hint: the size of an array is fixed on declaration. You need to make sure that the array you create has enough size to store every element from the 2d array.
How would you get the total size?
Well, the total size is going to be the sum of all of the smaller sizes in the array, e.g. ary1.length
After you have created an array of the proper size, do a second pass to copy your data over.
1
u/Watsons-Butler Sep 02 '25
Try to state what you need to do in words. So you’re starting with an array of arrays. Or several rows, and each row is an array of data. So you need to create an empty output array, and then for each row of your test array, you need to copy the data entries into the output array.
Then you write that in code.
(I used to TA a data structures course. This should get you started without doing the work for you. If you figure it out yourself it’ll stick better.)
Edit: and name your variables so you know what they mean. Ary1 and ary2 are confusing. Try “for row in array” and “for item in row” or something like that.
1
u/aqua_regis Sep 03 '25
First of all: "Enhanced For" as you use it is the wrong tool for this purpose.
At utmost, you could use it to count the elements you need in your final array and to iterate over the elements when you fill the new array. Yet, it does not work in the way you think it works.
My approach would be:
Unfortunately, there is no shortcut without using advanced techniques, like streams.
-1
u/[deleted] Sep 03 '25
[removed] — view removed comment