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]
# 1
> HI,> > I'm having some problems trying to make my 2D array> work. I can't figure out my problem.> ...What problem?
prometheuzza at 2007-7-12 22:16:08 > top of Java-index,Java Essentials,Java Programming...
# 2
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.
vopoa at 2007-7-12 22:16:08 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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.

prometheuzza at 2007-7-12 22:16:08 > top of Java-index,Java Essentials,Java Programming...
# 4

> > 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 > top of Java-index,Java Essentials,Java Programming...
# 5

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 > top of Java-index,Java Essentials,Java Programming...
# 6
I have found the origin of my problem.the problem is that Arrays.sort(); can only take 1D array not 2D array.Is there another alternative to sort my array in alphabetical format with a 2D array?
vopoa at 2007-7-12 22:16:08 > top of Java-index,Java Essentials,Java Programming...
# 7

> I have found the origin of my problem.

>

> the problem is that Arrays.sort(); can only take 1D

> array not 2D array.

>

> Is there another alternative to sort my array in

> alphabetical format with a 2D array?

Your data structure should not be a 2D array. It should be a 1D array of objects (as was suggested above). It just makes sense even without the sort, but with the sort, it is imperitive. In my mind, arrays should store only one kind of information. When you have what sounds like a record structure of information stored in an array, you should change this to objects.

petes1234a at 2007-7-12 22:16:08 > top of Java-index,Java Essentials,Java Programming...
# 8

> ...

> 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

prometheuzza at 2007-7-12 22:16:08 > top of Java-index,Java Essentials,Java Programming...