Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Manipulating Array Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Manipulating Array Data" Watch "Manipulating Array Data" New topic
Author

Manipulating Array Data

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1391
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1391
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Nov 16, 2004
Posts: 1391
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

Joined: Oct 27, 2005
Posts: 19544
    
  16

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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
Bartender

Joined: Oct 14, 2005
Posts: 18141
    
    8

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

Joined: Nov 16, 2004
Posts: 1391
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Nov 16, 2004
Posts: 1391
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

Yeah, if you are using 1.4, then it just looks like this:


Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1391
There seems to be somthing wrong with this line now:

return a1[0].compareTo(a2[0]);
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1391
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

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Oct 02, 2003
Posts: 10916
    
  12

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Manipulating Array Data
 
Similar Threads
Multiple select box problems
Newbie question... :(
Close JDialog without hide
Error occured while initialising for loop:
java.sql.SQLException: Closed Connection