Trouble with 2D array
HI,
I'm having some problems trying to make my 2D array work. I can't figure out my problem.
Here's my code:
package testo;
import java.util.Comparator;
import java.util.Arrays;
import java.util.*;
/**
*
* @author Owner
*/
publicclass Main
{
private String[][] TEST_DATA ={
{"3","TaskTracker","Add undo","Active","Unassigned",
"Feature","1.0","3 - Fix if time","","","Gary",
"6-9-2006",""},
{"2","TaskTracker","Icons need to be cleaned up","Active",
"Unassigned","Improvement","1.0","1 - Must Fix","","",
"Gary","15-7-2006",""},
{"1","TaskTracker","Clear doesn't clear the title","Closed",
"Alex","Bug","1.0","1 - Must Fix","22-6-2006","1 day",
"Sam","3-6-2006","..."}};
publicstaticvoid main(String[] args)
{
(new Main()).test();
}
privatevoid test()
{
System.out.println("- Original order -");
for (int a=0; a<TEST_DATA.length; a++)
{
for (int i = 0; i >< TEST_DATA.length; i++)
{
System.out.println(TEST_DATA[i][a]);
}
}
System.out.println("- Naturally sorted order -");
Arrays.sort(TEST_DATA);
for (int a=0; a<TEST_DATA.length; a++)
{
for (int i = 0; i >< TEST_DATA.length; i++)
{
System.out.println(TEST_DATA[i][a]);
}
}
StringLengthComparator stringLengthComparator =new StringLengthComparator();
Arrays.sort(TEST_DATA, stringLengthComparator);
System.out.println("- Unnaturally sorted order -");
for (int a=0; a<TEST_DATA.length; a++)
{
for (int i = 0; i >< TEST_DATA.length; i++)
{
System.out.println(TEST_DATA[i][a]);
}
}
}
publicclass StringLengthComparatorimplements Comparator
{
publicint compare(Object o1, Object o2)
{
return o1.toString().length() - o2.toString().length();
}
}
}
[4989 byte] By [
vopoa] at [2007-11-27 9:21:57]

> > Well, I'm trying to find the problem in my code.
> >
> > It gives me a "java.lang.ClassCastException"
> > -I did some reading on it, but I can't figure out
> my
> > problem.
>
> public int compare(Object o1, Object o2) // o1
> and o2 are String[], not String
> ... but don't you need to create some sort of custom
> class, like Task, which holds your data? You can then
> store (and compare) those Task objects.
Sorry I'm a bit confused.
when you said "// o1 and o2 are String[], not String"
-are you saying that o1 and o2 are not the 2D array from TEST_DATA?
Sorry, but kind of don't understand what your saying here:
but don't you need to create some sort of custom class, like Task, which holds your data? You can then store (and compare) those Task objects.
vopoa at 2007-7-12 22:16:08 >

Is there a simple way to fix my code?
I just want the 2D array tp be printed.
Here's my output on my IDE:
- Original order -
3
TaskTracker
Add undo
2
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.Comparable
TaskTracker
Icons need to be cleaned up
1
TaskTracker
Clear doesn't clear the title
- Naturally sorted order -
at java.util.Arrays.mergeSort(Arrays.java:1144)
at java.util.Arrays.sort(Arrays.java:1079)
at testo.Main.test(Main.java:41)
at testo.Main.main(Main.java:26)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
vopoa at 2007-7-12 22:16:08 >

> ...
> Sorry I'm a bit confused.
> when you said "// o1 and o2 are String[], not
> String"
> -are you saying that o1 and o2 are not the 2D array
> from TEST_DATA?
Sorry, I meant to say: o1 and o2 are of type String[], but need to be of type Comparable.
> Sorry, but kind of don't understand what your saying
> here:
> but don't you need to create some sort of custom
> class, like Task, which holds your data? You can then
> store (and compare) those Task objects.
Have a look at this document:
http://java.sun.com/docs/books/tutorial/java/concepts/class.html