AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sorted table

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
vertigo
Guest





PostPosted: Wed Dec 22, 2004 10:07 am    Post subject: sorted table Reply with quote



Hello

I need to build array with Integers which should be later sorted.
What container should i use ?

Thanx
Michal

Back to top
Stefan Schulz
Guest





PostPosted: Wed Dec 22, 2004 10:56 am    Post subject: Re: sorted table Reply with quote



On Wed, 22 Dec 2004 11:07:32 +0100
vertigo <ax178 (AT) wp (DOT) pl> wrote:

Quote:
Hello

I need to build array with Integers which should be later sorted.
What container should i use ?

That depends. If you know how many integers you'll get, a plain old
array is likely the best. Otherwise, and ArrayList (or any other List)
is the way to go.


--
In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
--- Rear Admiral Grace Murray Hopper

Back to top
vertigo
Guest





PostPosted: Wed Dec 22, 2004 11:18 am    Post subject: Re: sorted table Reply with quote





Quote:
That depends. If you know how many integers you'll get, a plain old
array is likely the best. Otherwise, and ArrayList (or any other List)
is the way to go.

but, how can i sort ArrayList ? I do not see any sorting function in
ArrayList class.
What should i look for ?

Thanx
Michal


Back to top
karlheinz klingbeil
Guest





PostPosted: Wed Dec 22, 2004 11:43 am    Post subject: Re: sorted table Reply with quote

vertigo schrub am Mittwoch, 22. Dezember 2004 12:18
folgendes:

Quote:
but, how can i sort ArrayList ? I do not see any
sorting function in ArrayList class.
What should i look for ?

ArrayList's Superclass Connection has a Sort() method.
All you have to do is to supply it with a "Comparator"
which acutally compares 2 items and tells whether on
is greater, less or equal

Her is an example:
--------------------------------------------

ArrayList liste;
Collator coll;
Comparator compInt;
Comparator compString;

public StichwortListe() {
super();
coll =
Collator.getInstance(Locale.getDefault());
compInt = new Comparator(){
public int compare(Object o1,Object o2){
return (((StichwortItem)o1).getID() -
((StichwortItem)o2).getID() );
}
};
compString = new Comparator()
{
public int compare(Object o1,Object o2){
String s1 =
((StichwortItem)o1).getName();
String s2 =
((StichwortItem)o2).getName();
return coll.compare(s1,s2);
}
};
}

/*
* As you can see, we have 2 Comparators, one compares
* an int field in an item, the other a string
* the "Collator" is locale-dependant, it gives the
* sort-order for Strings (which in German is different
* than in English for example.
*/



/*---------------------------------------------------------
* at last, you call Collections.Sort() with your
* ArrayList (here it is "this" for this class is
* derived from ArrayList) and the desired
* Comparator (String, in this case)
*---------------------------------------------------------
*/
public void sortItems(){
Collections.sort(this,compInt);
}


--------------------------------------------

Hope this helps...


--
greetz Karlheinz Klingbeil (lunqual)
http://www.lunqual.de oder http:www.lunqual.net

Back to top
VisionSet
Guest





PostPosted: Wed Dec 22, 2004 11:56 am    Post subject: Re: sorted table Reply with quote


"karlheinz klingbeil" <karlheinz-klingbeil (AT) despammed (DOT) com> wrote

Quote:
vertigo schrub am Mittwoch, 22. Dezember 2004 12:18
folgendes:

but, how can i sort ArrayList ? I do not see any
sorting function in ArrayList class.
What should i look for ?

ArrayList's Superclass Connection has a Sort() method.
All you have to do is to supply it with a "Comparator"
which acutally compares 2 items and tells whether on
is greater, less or equal


You mean Collections which is not a superclass of anything, but a utility
class for sorting, searching Collection classes.
Collection is the superinterface to most of the concrete container classes
and specifies no sorting abilities.
Note one is plural the other isn't.

--
Mike W



Back to top
IchBin
Guest





PostPosted: Wed Dec 22, 2004 12:04 pm    Post subject: Re: sorted table Reply with quote

vertigo wrote:
Quote:
Hello

I need to build array with Integers which should be later sorted.
What container should i use ?

Thanx
Michal

Something different. I wrote a method to take a vector and sort it by

converting to a sorted set, sort it and then copy back to vector and
return it. Should work just using the array code.

static Vector getSortedVector(Vector targetTree) {

SortedSet set = new TreeSet();
int setSize = targetTree.size();

// Add elements to the set
for (int i = 0; i < setSize; i++) {
set.add(targetTree.get(i));
}

// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
Object element = it.next();
}

// Sort elements
String[] array = (String[]) set.toArray(new String[set.size()]);

// Clean out vector before reading
targetTree.clear();
// Add elements back to vector
for (int i = 0; i < setSize; i++) {
targetTree.add(array[i]);
}
return (targetTree);
}

--


Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist

Back to top
Ryan Stewart
Guest





PostPosted: Wed Dec 22, 2004 12:36 pm    Post subject: Re: sorted table Reply with quote

"VisionSet" <spam (AT) ntlworld (DOT) com> wrote

Quote:
"karlheinz klingbeil" <karlheinz-klingbeil (AT) despammed (DOT) com> wrote in message
news:cqbmjl$4o2$1 (AT) online (DOT) de...
vertigo schrub am Mittwoch, 22. Dezember 2004 12:18
folgendes:

but, how can i sort ArrayList ? I do not see any
sorting function in ArrayList class.
What should i look for ?

ArrayList's Superclass Connection has a Sort() method.
All you have to do is to supply it with a "Comparator"
which acutally compares 2 items and tells whether on
is greater, less or equal


You mean Collections which is not a superclass of anything, but a utility
class for sorting, searching Collection classes.
Collection is the superinterface to most of the concrete container classes
and specifies no sorting abilities.
Note one is plural the other isn't.

Not to mention that Integer (which the OP wants to sort) already implements

Comparable, so writing a separate Comparator is totally redundant.



Back to top
Stefan Schulz
Guest





PostPosted: Thu Dec 23, 2004 7:11 am    Post subject: Re: sorted table Reply with quote

On Wed, 22 Dec 2004 07:04:16 -0500
IchBin <weconsul (AT) ptd (DOT) net> wrote:


Quote:
static Vector getSortedVector(Vector targetTree) {

SortedSet set = new TreeSet();

A Set must not contain duplicates. What if your original Vector
contained duplicate entries?

Quote:
int setSize = targetTree.size();

// Add elements to the set
for (int i = 0; i < setSize; i++) {
set.add(targetTree.get(i));
}

Even if you want to use a Set, why not just do a set.addAll(targetTree)?

Quote:
// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
Object element = it.next();
}

This loop is totally useless.

Quote:
// Sort elements
String[] array = (String[]) set.toArray(new
String[set.size()]);

Why bother using an Array here?

Quote:
// Clean out vector before reading
targetTree.clear();
// Add elements back to vector
for (int i = 0; i < setSize; i++) {
targetTree.add(array[i]);
}
return (targetTree);
}

This entire method can be replaced by
Collections.sort(targetTree);

--
In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
--- Rear Admiral Grace Murray Hopper

Back to top
IchBin
Guest





PostPosted: Thu Dec 23, 2004 8:29 am    Post subject: Re: sorted table Reply with quote

Stefan Schulz wrote:
Quote:
On Wed, 22 Dec 2004 07:04:16 -0500
IchBin <weconsul (AT) ptd (DOT) net> wrote:



static Vector getSortedVector(Vector targetTree) {

SortedSet set = new TreeSet();


A Set must not contain duplicates. What if your original Vector
contained duplicate entries?


int setSize = targetTree.size();

// Add elements to the set
for (int i = 0; i < setSize; i++) {
set.add(targetTree.get(i));
}


Even if you want to use a Set, why not just do a set.addAll(targetTree)?


// Iterating over the elements in the set
Iterator it = set.iterator();
while (it.hasNext()) {
Object element = it.next();
}


This loop is totally useless.


// Sort elements
String[] array = (String[]) set.toArray(new
String[set.size()]);


Why bother using an Array here?


// Clean out vector before reading
targetTree.clear();
// Add elements back to vector
for (int i = 0; i < setSize; i++) {
targetTree.add(array[i]);
}
return (targetTree);
}


This entire method can be replaced by
Collections.sort(targetTree);

Guess I should better start learning Collections..


--


Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.