简体   繁体   中英

keep original index of array (continued)

Hey guys thanks for your help previously especially to Sam I am, almost got this working however due to an family emergency had to stop working on it for a while, everything seems to be working except one bit which is keeping the original indicies:

int [] indices = new int [15];         
for(int i=0; i<indices.length; i++){indices[i] = i;} 

// Below code used for sorting
for (int i=0; i<distances.length; i++)
{
for(int j = i+1; j<distances.length; j++)
{
    if (distances[i] > distances[j])  
    {
         double temp = distances[j];
        distances[j] = distances[i];
        distances[i] = temp;                                      

         int tempindex = indices[i];
         indices[j] = indices[i];
         indices[i] = tempindex;                                       
    }            
 }       
 }

for (int i=1; i<distances.length; i++) 
{
 System.out.println("Point "+indices[i] + " -> " + distances[i]);
}

}

Sam I am did explain how to do it however seem to be having trouble getting the code to display the original index so does anyone have any ideas?

Don't know if I've understood your question. But I think there is an idex problem.

Thake a loot to the index you use when you assign tempindex.

int tempindex = indices[i];
indices[j] = indices[i];
// tempindex is equal to indeices[i], so this instruction does nothing.
indices[i] = tempindex; 

Try to change

int tempindex = indices[i];

To

int tempindex = indices[j];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM