Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Sub Arrays will not swap the way I want

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an assignment and the first part for me is to sort the arrays of Comparables. Then I am trying to sort the comparable[][] in ascending order by sub array, so the shortest sub array is first. There may be an easy way to do this but i did it the way i could figure out. I ended creating an int[] that contains the index locations of the sub arrays. The actual values in the intdex[] are not in order but correspond to the index locations in the Comparable[][]. I am simply trying move the sub array bases in the index[] location to a location in a sorted Comparable[][]. The problem is the last array swap. The values of the index locations are correct but the value os the sub array is wrong. I am not sure what is going on.
I left my working comments in, they may help to understand what I want to do and the see problem. Somewhere I am writing the value.




Main method







results.jpeg
[Thumbnail for results.jpeg]
consule output
 
Rancher
Posts: 904
22
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mark,
Upon a cursory inspection of your code, I can say there is a couple hours of reformatting, getting used to your style, and tracing out your problems. That just isn't going to happen, so...

Can you post the segment of code you think is giving you the problem and use code tags--and please make the formatting of your code a priority when you post, not to complain, but I can barely get my eyes to follow what is going with your posted code because of the current formatting.
Les
PS sorry not nearly as bad if I paste it into my IDE.

What is your desired output result?
you have:
[[0, 6, a, b, i, t, y8hq], [1, a, b, c, k, q, t, t], [0, 6, a, b, i, t, y8hq]]
are you saying it should be:
[[0, 6, a, b, i, t, y8hq], [0, 6, a, b, i, t, y8hq], [1, a, b, c, k, q, t, t]]
Or should there be no crossover between the arrays?
 
mark cortez
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mark cortez wrote:I am working on an assignment and the first part for me is to sort the arrays of Comparables. Then I am trying to sort the comparable[][] in ascending order by sub array, so the shortest sub array is first. There may be an easy way to do this but i did it the way i could figure out. I ended creating an int[] that contains the index locations of the sub arrays. The actual values in the intdex[] are not in order but correspond to the index locations in the Comparable[][]. I am simply trying move the sub array bases in the index[] location to a location in a sorted Comparable[][]. The problem is the last array swap. The values of the index locations are correct but the value os the sub array is wrong. I am not sure what is going on.
I left my working comments in, they may help to understand what I want to do and the see problem. Somewhere I am writing the value.




Main method









I figure it out. I needed to create a tempComp[] to store the values in then copy that to the correct location. I think I was overrate the compbarable[][] when I did tempComp[i] = collections2[indexArray[j]];.


Sorry about the formatting eclipse is auto-formatting on me.
 
Marshal
Posts: 70353
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

mark cortez wrote:. . . I figure it out. I needed to create a tempComp[] to store the values in then copy that to the correct location. . . .
Sorry about the formatting eclipse is auto-formatting on me.

The formatting looks all right to me, but please avoid the multiple /////////////
I am not convinced, I am afraid, that you have sorted it out. There is a difference between code which produces the expected result and something sorted out.
You mentioned the real solution in the beginning of your previous post:-

You wrote:the first part for me is to sort the arrays of Comparables

You have to do it inparts. One part: sort a small array. Second part: sort the other small arrays. Third part: look at the big array.

Have you written sorting code? No, you are using Arrays.sort, which is the right way to do it. So you sort the individual arrays, which is easy because the elements implement Comparable. Are they all the same type? You will get a nasty Exception if you include different types in the arrays. Then you need some way to compare arrays by length. That sounds like what appears in the Java™ Tutorials. Read that section, which is good and tells you about all sorts of pitfalls. Then all you need to do is pass the big array to Arrays.sort and some means to tell that the difference between the arrays' lengths. You should be able to work that out from the tutorial link. I do not believe there is any need to copy the arrays into somewhere else.
 
Whoever got anywhere by being normal? Just ask this exceptional tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic