473,326 Members | 2,337 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

HELP!! About 2D and 1D arrays

I'm matching the value of each of the 2D array against 1D Array. 1D array is actually the conversion of 2D to 1D array, so that i can use Arrays.sort() to sort the array to ascending order. However when i'm printing out, it give me extra values. I should have 6 values printed out only.
Expand|Select|Wrap|Line Numbers
  1. In score 2D array:
  2. [ i ] [ j ]    [ value ]
  3.   0    0          1.0
  4.   0    1          0.04
  5.   1    0          0.24 
  6.   1    1          1.0
  7.   2    0          0.15
  8.   2    1          0.14
Expand|Select|Wrap|Line Numbers
  1. Before sorting newArray 1D array:
  2.  [ a ]          [ value ]
  3.    0               1.0
  4.    1               0.24
  5.    2               0.24
  6.    3               1.0
  7.    4               0.15
  8.    5              0.14
Expand|Select|Wrap|Line Numbers
  1. After sorting newArray 1D array:
  2.  [ a ]           [ value ]
  3.    0               0.14
  4.    1               0.15
  5.    2               0.24
  6.    3               0.24
  7.    4               1.0
  8.    5               1.0
Expand|Select|Wrap|Line Numbers
  1. The output is:
  2. [ i ] [ j ]      newArray[a] 
  3.   2    1            0.14
  4.   2    0            0.15
  5.   0    1            0.24
  6.   1    0            0.24
  7.   0    1            0.24  //this is repeated
  8.   1    0            0.24  //this is repeated
  9.   0    0             1.0
  10.   1    1             1.0
  11.   0    0             1.0   //this is repeated
  12.   1    1             1.0  //this is repeated
Expand|Select|Wrap|Line Numbers
  1. queryIDArray(length of 3) and srnIDArray(length of 2) are all 1D arrays.
  2. public static void sortArray(float[][] score)
  3. {
  4.     int k=0;
  5.  
  6.     float[] newArray = new float[queryIDArray.length*scrnIDArray.length];
  7.  
  8.     for(int i=0;i<queryIDArray.length;i++)
  9.     {
  10.         for(int j=0;j<scrnIDArray.length;j++)
  11.         {
  12.             newArray[k] = score[i][j];
  13.             k++;
  14.         }                                      
  15.     }
  16.  
  17.     Arrays.sort(newArray);
  18.     for(int a=0;a<newArray.length;a++)
  19.     {
  20.         for(int i=0;i<queryIDArray.length;i++)
  21.         {
  22.             for(int j=0;j<scrnIDArray.length;j++)
  23.             {
  24.  
  25.                 if(newArray[a]==score[i][j]) 
  26.                     System.out.println(i+" "+j+" "+newArray[a]);
  27.                     //what condition should I put so that it will not repeat?
  28.  
  29.             }                                      
  30.         }
  31.     }
  32. }
How can I check if the value is printed out once, it would not print it out again?
Feb 2 '07 #1
3 2451
horace1
1,510 Expert 1GB
couple of notes
you have a value 0.04 in the initial 2D which appears to have been replaced with 0.24 in the 1D array.
you get repeats such as
Expand|Select|Wrap|Line Numbers
  1.   0    0             1.0
  2.   1    1             1.0
  3.   0    0             1.0   //this is repeated
  4.   1    1             1.0  //this is repeated
  5.  
because 1.0 appears more than once in the original array. Could you make a copy of the sorted array and remove duplicates? not sure what you are trying to do?
Feb 2 '07 #2
couple of notes
you have a value 0.04 in the initial 2D which appears to have been replaced with 0.24 in the 1D array.
you get repeats such as
Expand|Select|Wrap|Line Numbers
  1.   0    0             1.0
  2.   1    1             1.0
  3.   0    0             1.0   //this is repeated
  4.   1    1             1.0  //this is repeated
  5.  
because 1.0 appears more than once in the original array. Could you make a copy of the sorted array and remove duplicates? not sure what you are trying to do?
There is an error in my typing, it should be
Expand|Select|Wrap|Line Numbers
  1. In score 2D array:
  2. [ i ] [ j ]    [ value ]
  3.   0    0          1.0
  4.   0    1          0.24 //should be 0.24 instead of 0.04
  5.   1    0          0.24 
  6.   1    1          1.0
  7.   2    0          0.15
  8.   2    1          0.14
Instead of removing duplicates, how can i check when matching 1D (i.e. newArray) against 2D (i.e. score), check that when it matches both positions of 1.0 (at i=0, j=0 and at i=1, j=1) then it will stop printing?
Feb 5 '07 #3
There is an error in my typing, it should be
Expand|Select|Wrap|Line Numbers
  1. In score 2D array:
  2. [ i ] [ j ]    [ value ]
  3.   0    0          1.0
  4.   0    1          0.24 //should be 0.24 instead of 0.04
  5.   1    0          0.24 
  6.   1    1          1.0
  7.   2    0          0.15
  8.   2    1          0.14
Instead of removing duplicates, how can i check when matching 1D (i.e. newArray) against 2D (i.e. score), check that when it matches both positions of 1.0 (at i=0, j=0 and at i=1, j=1) then it will stop printing?
Here is the solution:

Expand|Select|Wrap|Line Numbers
  1. queryIDArray(length of 3) and srnIDArray(length of 2)
  2. int[] xArray = new int[queryIDArray.length*scrnIDArray.length];
  3. int[] yArray = new int[queryIDArray.length*scrnIDArray.length];
  4. float[] valueArray = new float[queryIDArray.length*scrnIDArray.length];
  5.  
  6. int k=0;
  7.  
  8. for(int i=0;i<queryIDArray.length;i++)
  9. {
  10.     for(int j=0;j<scrnIDArray.length;j++)
  11.     {
  12.         xArray[k]=i;
  13.         yArray[k]=j;
  14.         valueArray[k]=score[i][j];
  15.  
  16.         k++;
  17.     }
  18. }
  19.  
  20. ArrayList descIndex = new ArrayList(queryIDArray.length*scrnIDArray.length);
  21.  
  22.  
  23. float temp=-0.1F;
  24.  
  25. int highestIndex=0;
  26.  
  27. for(int z=0;z<k;z++)
  28. {
  29.     temp=-0.1F;
  30.     for(int m=0;m<valueArray.length;m++)
  31.     {
  32.  
  33.         if(descIndex.indexOf(m)==-1)
  34.         {
  35.             if(valueArray[m]>temp)
  36.             {
  37.                         temp=valueArray[m];
  38.  
  39.                         highestIndex=m;
  40.             }
  41.         }
  42.  
  43.     }
  44.     descIndex.add(highestIndex);
  45. }
The above solution will sort all the scores in the file to descending order.
Feb 17 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Dariusz | last post by:
I want to use arrays in my website (flat file for a guestbook), but despite having read through countless online tutorials on the topic, I just can't get my code to work. I know there are...
19
by: Canonical Latin | last post by:
"Leor Zolman" <leor@bdsoft.com> wrote > "Canonical Latin" <javaplus@hotmail.com> wrote: > > > ... > >But I'm still curious as to the rational of having type >...
4
by: Mingus Tsai | last post by:
Hello- please help with unpickling problem: I am using Python version 2.3.4 with IDLE version 1.0.3 on a Windows XPhome system. My problem is with using cPickle to deserialize my pickled...
2
by: Pasacco | last post by:
dear I want to ask help on this problem. Array a is partitioned into a0 and a1 in main(). Then a1 is partitioned into a2 and a3 in th_partition() function. And I think this problem is something...
5
by: JezB | last post by:
What's the easiest way to concatenate arrays ? For example, I want a list of files that match one of 3 search patterns, so I need something like DirectoryInfo ld = new DirectoryInfo(searchDir);...
10
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
3
by: alternative49e | last post by:
Windows XP JAVA n00b here. I'm not very good at Java. In fact I am really struggling with it. I've spent the last 6 hours trying to get this to work. The book I have isn’t very helpful. I'm...
5
by: saytri | last post by:
Hi i have this project were i have to do a quiz. i wrote the questions in a textfile and i called them through java. I have also made a menu to choose which type of quiz. But before accessing the...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.