Home arrow static arrow Java Programming [Archive] - assigning values to a multidimensional array
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - assigning values to a multidimensional array
This topic has 40 replies on 3 pages.    1 | 2 | 3 | Next »

Posts:114
Registered: 7/5/04
assigning values to a multidimensional array  
Jul 17, 2004 7:02 AM



 

hi

I' have to make up a table which contains 3 columns by 70 lines.i'm not sure what to use to hold the contents, fundamental array, hashtable, arryList.?maybe a mix?

And I don�t know how to assign values to multidimensional arrays. For example I have to assign the first line to this table, lets say its :

Group = 10 , number = 34 , serial = 45

What is the right syntax to do it?

Thanks
 

Posts:18,384
Registered: 21.03.00
Re: assigning values to a multidimensional array  
Jul 17, 2004 7:09 AM (reply 1 of 40)



 
Hi,

How would you display the values? Can the number of rows decrease/increase? If you are going to display them in swing, look at the JTable and the DefaultTableModel. You can hold the data in a Vector of Vectors, or an array declared as Object[][].

Could you specify what you should do with the array?

/Kaj
 

Posts:114
Registered: 7/5/04
Re: assigning values to a multidimensional array  
Jul 17, 2004 7:23 AM (reply 2 of 40)



 
I'll receive unsorted data which I'll sort out by algorithm I'll write. But first I like to organize all the data in 3 columns. For example if I'll get the value :"23,45,12" I like to sort it like this in an array :

Group = 23
Number = 45
Serial = 12.

But the sorting isn�t bothering me right now.the problem is how to assign those numbers to the columns? I like that the group will become the key and the Number and Serial will become the value. How can I do it?

Thanks
 

Posts:11,186
Registered: 06.04.04
Re: assigning values to a multidimensional array  
Jul 17, 2004 8:49 AM (reply 3 of 40)



 
Your data is not just multi dimensional data, i.e. values in a row have a different meaning: group, number
and serial. This begs for a little class like this --
class DataItem {   // the data   private int group;   private int number;   private int serial;   // ctor   public DataItem(int group, int number, int serial) {      this.group= group;      this.number= number;      this.serial= serial;   }   // accessors   public int getGroup() { return group; }   public int getNumber() { return number; }   public int getSerial() { return serial; }   // handy to have   public String toString() {      return "group: "+group+" number: "+number+" serial: "+serial;   }} 

Whenever you receive three numbers, build a new DataItem(group, number, serial) and store it
somewhere in an ArrayList or another convenient collection. Have a look at the Comparator
interface when you want to sort your DataItems according to their group, number or serial values.

Don't use a two dimensional array for this.

kind regards,

Jos
 

Posts:114
Registered: 7/5/04
Re: assigning values to a multidimensional array  
Jul 17, 2004 10:06 AM (reply 4 of 40)



 
Thanks for your reply!

i made a DataItem Class *** you write it. in my main class when i try to put the values in a hashtable i get error:

"TestClassFrame.java": cannot resolve symbol: method put (int,Tests.DataItems)in class java.util.Hashtable at line 114, column 11

the relevant code is :

nt[] arr1 = {32,67,34};
int[] arr2 = {22,62,92};
Hashtable has = new Hashtable();

void jButton4_actionPerformed(ActionEvent e) {

for(int i=0;i<arr1.length;i++){
has.put(i,new DataItems(arr1[1],arr1[2],arr1[3]));
}

}

it seems like the Hashtable doesnt get the DataItem class as a liggle new type,why?

Thanks
 

Posts:11,186
Registered: 06.04.04
Re: assigning values to a multidimensional array  
Jul 17, 2004 11:26 PM (reply 5 of 40)



 
"TestClassFrame.java": cannot resolve symbol: method put (int,Tests.DataItems)in class
java.util.Hashtable at line 114, column 11

Hashtable has = new Hashtable();
for(int i=0;i<arr1.length;i++){
has.put(i,new DataItems(arr1[1],arr1[2],arr1[3]));
}

This doesn't make sense; have a look at the API for HashTable: the put method takes two Objects;
one for the key and one for the associated value. You're trying to pass an int for the key which isn't
an Object; it's a primitive type; the DataItem would do fine as an associated value though .

But why stick those DataItems in a HashTable? A simple ArrayList would do fine.

kind regards,

Jos
 

Posts:114
Registered: 7/5/04
Re: assigning values to a multidimensional array  
Jul 18, 2004 2:25 AM (reply 6 of 40)



 
Hi

Sorry I post the wrong code but anyway I used ArrayList instead of Hashtable and worked fine.

Now after creating the class DataItems and inserting all the data into ArrayList its time to sort it by label. I could of writing primitive algorithm to sort it but I saw here that there is a special class for this purpose.

Here is the code of the DataItem class with minor changhes:

public class DataItems {   int group=0;  String number;  int label=0;   //get the values and assign them to this varbs  public DataItems(int group,String number,int label){      this.group = group;      this.number= number;      this.label= label;  }  public DataItems(){}public int getGroup(){    return group;  }   public String getNumber(){    return number;  }   public int getlabel(){    return label;  }   public String toString(){    return new String("group: "+this.group+" number: "+this.number+" label: "+this.label);  }}


in my main class i use this code to insert all the data to the array

void SortByLabel(String num,int label){     //store all data into array    for(int i=0;i<71;i++){      table.add(i,new DataItems(i,num,label));    }   } 


After processing this code the data into the array it should look like this:

1,(group :45, number: 23, label: 2)
2,(group :56, number: 12, label: 1)
3,(group :67, number: 12, label: 3)

what I want to do is to sort it out by the label value :

1,(group :56, number: 12, label: 1)
2,(group :45, number: 23, label: 2)
3,(group :67, number: 12, label: 3)

how can I do it?

Thanks
 

Posts:18,384
Registered: 21.03.00
Re: assigning values to a multidimensional array  
Jul 18, 2004 3:18 AM (reply 7 of 40)



 
Hi,

You should use a TreeSet instead of an ArrayList. The tree set sorts its data when you add it to the set, you just have to pass your own Comparator to the constructor of the TreeSet.

/Kaj

Ps.
Are you sure that the method SortByLabel is correct? Each time you call it it adds 71 records with the same num and label.
 

Posts:114
Registered: 7/5/04
Re: assigning values to a multidimensional array  
Jul 18, 2004 3:31 AM (reply 8 of 40)



 
Thanks!
yes you right, i write it just to demonstrate my problem.

i'll look for TreeSet....it sound good!

Thanks again!
S
 

Posts:114
Registered: 7/5/04
Re: assigning values to a multidimensional array  
Jul 18, 2004 3:40 AM (reply 9 of 40)



 
ohhhhh....its not easy to understand...

can u give me en example?
 

Posts:11,186
Registered: 06.04.04
Re: assigning values to a multidimensional array  
Jul 18, 2004 3:47 AM (reply 10 of 40)



 
i'll look for TreeSet....it sound good!

No it doesn't sound good: a TreeSet doesn't allow for duplicate values and a TreeSet orders its elements
in just one particular order. You'll need three TreeSets if you want to order your DataItems according to
their different data members. Read the API for Comparator and simply use the Collections.sort method
using an appropriate Comparator.

kind regards,

Jos
 

Posts:114
Registered: 7/5/04
Re: assigning values to a multidimensional array  
Jul 18, 2004 4:00 AM (reply 11 of 40)



 
THanks

so i need all the members of my DataItems to become TreeSets....right? so the comperator is just a sunscript i should write and define the way i want my members to be sorted?

if so where i can find example of code that does such of thing?

Thanks again
S
 

Posts:18,384
Registered: 21.03.00
Re: assigning values to a multidimensional array  
Jul 18, 2004 4:09 AM (reply 12 of 40)



 
Do you only have to sort it on label, or do you want to be able to sort on any column?

/Kaj
 

Posts:11,186
Registered: 06.04.04
Re: assigning values to a multidimensional array  
Jul 18, 2004 4:18 AM (reply 13 of 40)



 
so i need all the members of my DataItems to become TreeSets....right?

Wrong; as I wrote above a TreeSet doesn't allow duplicate entries. Use an ArrayList (or another List)
to store your DataItems. Write three Comparators, one for each field in the DataItems and pass it
to the Collections.sort method. Here's one Comparator, it compares the 'group' field --
public class GroupComparator implements Comparator {   public int compare(Object a, Object b) {      DataItem da= (DataItem)a;      DataItem db= (DataItem)b;      int ga= da.getGroup();      int gb= db.getGroup();       if (ga < gb) return -1;      if (ga > gb) return  1;      return 0;   }   public boolean equals(Object obj) return obj instanceof GroupComparator; }} 


This should get you started ...

kind regards,

Jos
 

Posts:114
Registered: 7/5/04
Re: assigning values to a multidimensional array  
Jul 18, 2004 4:22 AM (reply 14 of 40)



 
Thanks a lot Jos :)
 
This topic has 40 replies on 3 pages.    1 | 2 | 3 | Next »