• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to flatten 2D array into 1D array

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to flatten a 2d array and make it into a 1d array. The code I have seems to be compiling but the output is strange. I get a [I@45ee12a7 for some reason. Can someone explain why that would be? I've seen that output before when something was wrong, but just ended up playing with it until it was fixed and never actually figured out the reason for this. Also can someone please help explain how to flatten a 2d array?

 
Ranch Hand
Posts: 310
18
MS IE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't output an array directly like this. It's toString() will output you its hash code.

Use java.util.Arrays.toString() to get a nice String representation of an array.

By the way, this code will not work as expected ;-)
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is nothing strange about that output, only unfamiliar to you. Array classes don't override Object.toString() and that is the usual output. If you want to print the contents of your array try this or this.
Please explain what you mean by flattening arrays; when you have done that you will probably have solved your problem by yourself
 
Jeff Sak
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see what you mean that it wasn't the proper output. The Arrays.toString() did help me get the numbers to output, but it didn't show the last number in each of the array elements. I am trying to turn array c2 = {{8, 3, 5, 7}, {2, 5, 0, 1}, {3, 9, 6, 4}} into a 1d array = {8, 3, 5, 7,2, 5, 0, 1,3, 9, 6, 4}, but instead the last 3 digits are 0 and it doesn't output the 7, 1, or the 4.
 
Andrew Polansky
Ranch Hand
Posts: 310
18
MS IE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem doesn't lie in Arrays.toString(), but in your flatten() method. As a proof, try to print your result like this and you will get exactly the same values as from Arrays.toString().


Try to work on your flatten() method a little more and try to find what is wrong
 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't worry, it is quite difficult at the beginning to understand about the array of arrays.

Ok, lets try to take this code
And make it look like this (note: row[*] and col[*] are illegal there - these are just for visualisation)
Now look at the two array declarations and initializations. Noticed, that these two are exactly the same, just different indentation and formatting basically. When you create array of arrays, I'd suggest you to use latter style (once again, just without those row and col).

When you have two nested loops, with an outer loop you iterate over the rows, with an inner over the columns OR in different words, outer loop iterates over the elements within the first deep level {...}, the inner loop over the elements within the second deep level {{...}}. That could sound confusing, read further.

Now what is happening when you iterating through an array with one loop?
Remember, Arrays.toString() method prints whole array in one go. Earlier I mentioned that 2D arrays how you call them, in Java these are array of arrays. So when you run your outer loop, you get array elements which are in between {...} first deep level brackets. In other words you get 3 elements which are also arrays:
{8, 3, 5, 7}
{2, 5, 0, 1}
{3, 9, 6, 4}

In order to get elements of those 3 arrays, you need another loop which is nested. You got that, but your problems started exactly there. Now the question is why?

Look carefully and you'll notice, that outer array (this is how i call it temporary here) has 3 elements in it (3 another arrays), BUT these 3 arrays, each of them has by 4 elements in it.
When you write arr.length, you get length of outer array (for that outer someone will censure me), which is length 3, right?
Your inner loop has exactly the same as outer arr.length, WHICH means - you'll iterate 1st element of array (which is also array {8, 3, 5, 7}) at most 3 times. BUT you need 4 here, get the idea where the problem is?

SOMEHOW, you need to amend inner for loop condition to iterate over the length of the {8, 3, 5, 7}.
Remember, at positions:
  • arr[0] there is an array element {8, 3, 5, 7} which is array itself
  • arr[1] there is array element {2, 5, 0, 1} which is array itself
  • arr[2] there is array element {3, 9, 6, 4} which is array itself

  • Look once again to your loops:
    So, how do you need to amend one of the loops so you could iterate with outer loop over the rows length and inner over the columns length (one of those underlined you're doing correct already)?
     
    Campbell Ritchie
    Marshal
    Posts: 79151
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You also need to check that your element arrays are all the same length; that is called rectangular array. They can be different lengths, which is sometimes called a jagged array. You may need to add all the individual arrays' lengths.

    Are you allowed to use a List?
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jeff Sak wrote:Also can someone please help explain how to flatten a 2d array?


    FYI, in version 8 there is a much simpler way to do it using Streams, viz:At least I assume it works. I've never tried it.

    Winston
     
    Danger, 10,000 volts, very electic .... tiny ad:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic