0

I had to turn nested ArrayList

 List<List<TockaXY>> clustersPorazdeljeni = new ArrayList<>(center.size()) 

in to Array, witch with help of nice people from this site is now clear:

TockaXY[] arrayOfClusters = clustersPorazdeljeni.stream().flatMap(Collection::stream).toArray(TockaXY[]::new);

But I have to also do the reverse. So how to get from arrayOfClusters back an nested ArrayList like

List<List<TockaXY>> newClustersPorazdeljeni 

?

5
  • 1
    You can't if you don't know the length of each ArrayList. Commented Sep 7, 2019 at 20:24
  • 1
    Notice that we don't know how original nested list looked like. If you had nested lists like [[foo1,foo2],[foo3]] or [[foo1],[foo2,foo3]] after turning them into 1 dimensional array you will get [foo1, foo2, foo3] from both of them, so we can't simply turn such array back into original list because we don't have any way to determine which version of nested list was used to create that array. Commented Sep 7, 2019 at 20:31
  • 1
    For now your question looks like XY problem and before providing more details we can't give you correct answer for your specific case. We can give you only answers which would be guesses based on some assumptions which may not be true. Commented Sep 7, 2019 at 20:38
  • public TockaXY(float x, float y) { this.x = x; this.y = y; } Commented Sep 8, 2019 at 7:31
  • List<List<TockaXY>> clusters = new ArrayList<>(centers.size()); for (int i = 0; i < centers.size(); i++) { clusters.add(new ArrayList<TockaXY>()); Commented Sep 8, 2019 at 7:31

1 Answer 1

2

You have asked vice versa question today. How to convert nested ArrayList in Array?

TockaXY[] arrayOfClusters this is flat array and there is no mark how to split it to a few arrays. Maybe you should to do the convertation to another data structure not in TockaXY[], to map en example.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes! That is me. The trick is I have to convert nested ArrayList in to Array to pass it in MPI.COMM_WORLD.Scatter. There I have an method that call's another method that uses ArrayList. But to pass back with MPI.COMM_WORLD.Gather I again need Array. I think I will rewrite the methods that use ArrayList to different data structure. Thank for your time! Appreciate it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.