Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Manipulating Array Data

 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am creating an array and populating the elements with values while looping through a ResultSet.

My two questions are: How can I sort the array and out of the thirty items I only need the top 8 values.

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

If possible, I'd let the database do the work for you. Add an "ORDER BY ... DESC" clause to do the sorting, and a "LIMIT 8" clause to just grab the top 8 items.

If you can't change the query, then you can sort defectcodearray using java.util.Arrays.sort() and an appropriate java.util.Comparator implementation.
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:Hi Steve,

If possible, I'd let the database do the work for you. Add an "ORDER BY ... DESC" clause to do the sorting, and a "LIMIT 8" clause to just grab the top 8 items.

The item list is built on the fly. I have one file with a list of generic codes. I iterate through the list finding matches and getting a count from second file field where the code(s) are located.

What java.util.Comparator implementation should I use? I keep getting ClassCastException by just using Arrays.sort(this.defectcodearray);
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:
What java.util.Comparator implementation should I use? I keep getting ClassCastException by just using Arrays.sort(this.defectcodearray);


Right, the ClassCastException is because without passing a Comparator, the argument has to be an array of items which implement Comparable, which array-of-String does not.

You have to write a Comparator yourself. It's really easy here, because you just want it to compare the defectcodearray[][0] items (or [1] or [2], you haven't said which,) and String has a compareTo() method you can use. It could just be as simple as



So then you pass an instance of this as the second argument to sort(), and Bob's your uncle.
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for asking so many questions.

Do I put this compare class inside my class that I ma building the array in. Also what goes in the <String[]> part of the class?

I am want the defectcodearray[][0] elements sorted
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:and a "LIMIT 8" clause to just grab the top 8 items.

Be careful with that - it's a MySQL only technique. MS SQL Server for instance uses a TOP X directly after the SELECT keyword:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:Sorry for asking so many questions.

Do I put this compare class inside my class that I ma building the array in. Also what goes in the <String[]> part of the class?

I am want the defectcodearray[][0] elements sorted


No problem about all the questions -- happy to help.

You put this class either into its own file, or in the same file but at the end, after the other class definition, or you could even nest it inside the other class, if you wanted. Doesn't matter.

The <String[]> part is actual code -- it's part of Java's "generics" facility.

Sounds like maybe I'm getting a little ahead of you here; sorry. So you just need to do something like

Arrays.sort(defectcodearray, new MyComparator());

Rob:

Thanks, I didn't actually know that. Postgres supports it too, and I've never used SQL Server (lucky me, I think).


 
Paul Clapham
Sheriff
Posts: 21312
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:
Ernest Friedman-Hill wrote:and a "LIMIT 8" clause to just grab the top 8 items.

Be careful with that - it's a MySQL only technique. MS SQL Server for instance uses a TOP X directly after the SELECT keyword:


I notice that Statement has a setMaxRows() method. I vaguely recall using this in the past to return just one record from a potentially large ResultSet.
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest Friedman-Hill wrote:
Steve Dyke wrote:Sorry for asking so many questions.

No problem about all the questions -- happy to help.

or you could even nest it inside the other class, if you wanted. Doesn't matter.

Sounds like maybe I'm getting a little ahead of you here; sorry. So you just need to do something like



I would really rather use your, embed in my existing class, suggestion. However, I feel like the code for the class will be different and I cannot figure out how to add the to my class in its current form. When I just copy the code in I get the following

Class must implement the inherited abstract method Comparator.compare(Object, Object)

Thanks again for all the help.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you including the "<String[]>" part, just as written here?

Here's a complete program that compiles (but doesn't do anything) as an example. It does require Java 5 or later -- won't work with JDK 1.4, you're not using that, are you?

 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know I think I am using JDK 1.4.

I am using Websphere to develope web apps. This all runs on our iSeries/AS400 box.

If this is case is there any hope for sorting an array of string objects?

How can I convert the string objects into items that Arrays can sort?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, if you are using 1.4, then it just looks like this:


 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There seems to be somthing wrong with this line now:

return a1[0].compareTo(a2[0]);
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think something is wrong with that line?

If you are getting a compiler error, please post the full and exact text - it really is useful information.

If there is some other issue, please explain what that is.
 
Steve Dyke
Ranch Hand
Posts: 1642
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:Why do you think something is wrong with that line?

If you are getting a compiler error, please post the full and exact text - it really is useful information.

If there is some other issue, please explain what that is.


Sorry about that. I am getting

The type of the expression must be an array type but it resolved to Object

On the a1[0] and the a2[0]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, Steve. Without the generics, you have to assume that the Object arguments are really whatever type you think they're supposed to be, and cast them. So the compareTo method actually has to do this:

return ((String[]) a1[0]).compareTo(((String[]) a2)[0]);
 
fred rosenberger
lowercase baba
Bartender
Posts: 12183
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Dyke wrote:
Sorry about that.

No worries. We're just trying to help you learn how to write better posts here, in addition to helping your Java.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic