LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-14-2010, 08:01 PM   #1
Asido
Member
 
Registered: Jan 2010
Location: Denmark
Distribution: Gentoo, Archlinux, FreeBSD, Slackware
Posts: 84

Rep: Reputation: 24
Array to List, sorting (Java)


Hello,
Can anyone tell me what is wrong with this:

Code:
CardBase[] tmp = new CardBase[13];
System.arraycopy(deck, range*13, tmp, 0, 13);
Collections.sort(Arrays.asList(tmp));
In this place the compiler gives me an error:
Code:
cannot find symbol
symbol  : method sort(java.util.List<CardBase>)
location: class java.util.Collections
                Collections.sort(Arrays.asList(tmp));

Last edited by Asido; 08-15-2010 at 12:49 PM.
 
Old 08-14-2010, 10:28 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

There are two problems here:

PROBLEM 1:
Before you can "sort" something, it has to be "Sortable".

More specifically, the items in the "container" you want to sort need to implement class "Comparable".

Here's an example: we create a "list" of type "String"; "String" implements "Comparable" (by default) ... and everything works:
Code:
import java.util.*;

public class Sort {
    public static void main(String[] args) {
        List<String> list = Arrays.asList(args);
        Collections.sort(list);
        System.out.println(list);
    }
}
Quote:
javac Sort.java
java Sort I Walk the Line

[i, line, the, walk]
PROBLEM 2:
"Containers.sort()" can sort a list ...
... but not an array.

If you've got an array, you should use "Arrays.sort()" instead.

I noticed you tried to convert via "Arrays.asList()" ... but that incurs extra overhead. LOTS of extra overhead. And it didn't work

Soooooo ...

If you implemented "Comparable" in your class "CardBase" ...
... and if you used "Arrays.sort()" instead of "Containers.sort ()"
... then it *should* work (if everything else is OK)

Please read any of these links - they might help explain better than me:
http://download-llnw.oracle.com/java...hms/index.html

http://www.exampledepot.com/egs/java...SortArray.html

http://www.java-examples.com/sort-el...aylist-example

'Hope that helps .. PSM

Last edited by paulsm4; 08-14-2010 at 10:33 PM.
 
Old 08-15-2010, 11:49 AM   #3
Asido
Member
 
Registered: Jan 2010
Location: Denmark
Distribution: Gentoo, Archlinux, FreeBSD, Slackware
Posts: 84

Original Poster
Rep: Reputation: 24
That was very informative, thank you.
Replacing the error line with Arrays.sort(tmp) solved the problem.

By the way, in one of your link I saw pretty much the same way of sorting as I did:
Code:
String[] strArray = new String[] {"z", "a", "C"}; 
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
Collections.reverse(Arrays.asList(strArray));
Speaking about Comparable implementation, CardBase is actually an interface which looks like this:
Code:
public interface CardBase {
	public int compareTo(Object o);
	public Suit setSuit();
	public Suit getSuit();
	public String getShortSuit();
}
The deck array which is being copied with System.arraycopy method to a tmp array is:
Code:
CardBase[ ] deck = new Card[52];
Card class is the class which implements CardBase and Comparable interfaces. So from my point of view everything should be ok, but still isn't.

Last edited by Asido; 08-15-2010 at 12:39 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
[SOLVED] how to split string into an array/list of lines in C++? rohedin Programming 11 06-06-2010 10:54 AM
Java 2D Array jabfinger Programming 12 12-13-2009 04:55 PM
list with dynamic array TurtleFace Programming 7 11-05-2006 06:58 PM
Is there a default size limit to a dictionary, list or array declaration ? Linh Programming 2 06-08-2004 11:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:56 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration