Why are the array indexes not being swapped?

浪子不回头ぞ 提交于 2020-02-07 17:12:36

问题


I am trying to get an array position to be swapped and it will not work. No matter what I do, I cannot get them to be swapped, I know it has something to do with the way I have the if statement written.

Here is the code in the main :

  Comparable[] computerSizes = new Comparable[3];

   int a = computerSizes.length - 1;



   computerSizes[0] = new Desktop("i5","Desktop",4,1024,250); 
   computerSizes[1] = new Desktop("i3","Desktop",6,512,350); 
   computerSizes[2] = new Laptop(15.6,"i3","Laptop",4,0,750); 

   for (int i = 0; i < a;i++) {

     if(computerSizes[i].compareTo(computerSizes[i+1]) == 1){
          computerSizes[i] = computerSizes[i+1];
          computerSizes[i+1] = computerSizes[i];
        }//end if
     System.out.println(computerSizes[i]);
    }//end for

Here is the relevant compareTo method code:

 public int compareTo(Comparable c)
{   
    Computer a = (Computer)c;


     if (this.cost == a.cost)
        return 0;
     else if (this.cost > a.cost)
        return 1;
     else
        return -1;



}

Index at 0 is greater than index at 1 but just for clarification I will include relvant formula which is:

cost = 150 + 6.50 * super.ram + 0.15 * super.hdd + 0.48 * super.vRam;

Desktop(PROCESSOR,TYPE,RAM,VRAM,HDD SPACE): this is what the parameters mean.


回答1:


The lines

computerSizes[i] = computerSizes[i+1];
computerSizes[i+1] = computerSizes[i];

don't do what you expect. It first assign the value of computerSizes[i+1] to computerSizes[i]. At that moment, both are equal. Then you assign the value of computerSizes[i] to computerSizes[i+1]. At the end, both will be equal.

In order to swap the values, use a temporal variable:

Comparable temp = computerSizes[i];
computerSizes[i] = computerSizes[i+1];
computerSizes[i+1] = temp;



回答2:


Use a Temp Comparable to do SWAP

  Comparable tempComparable;
  if(computerSizes[i].compareTo(computerSizes[i+1]) == 1){
          tempCoparable = computerSizes[i];
          computerSizes[i] = computerSizes[i+1];
          computerSizes[i+1] = tempComparable;
        }//end if



回答3:


Let us consider an array : new int[3]{ 1, 2, 3 };

What you are doing is:

a[i] = a[i+1];
a[i+1] = a[i];

which does (let i = 0):

1. {1,2,3}   
2. {2,2,3}   |a[0] i.e. 1 gets replaced by 2
3. {2,2,3}   |a[1] i.e. 2 gets replaced by 2 (though it must be replaced by 1)

So, there must be a way to keep a temporary variable. Let's consider the following:

Computer temp = computerSizes[i];
computerSizes[i] = computerSizes[i+1];
computerSizes[i+1] = temp;

This will give the required result.




回答4:


Swapping code is incorrect. It should be

  if(computerSizes[i].compareTo(computerSizes[i+1]) == 1){
              Computer temp = computerSizes[i]
              computerSizes[i] = computerSizes[i+1];
              computerSizes[i+1] = temp ;
            }//end if



回答5:


As various answers say, your swap code won't work because you have no intermediate variable

      myarray[i] = myarray[i+1];  // both i and i+1 now refer to the same object
      myarray[i+1] = myarray[i];  // so this line has no effect

If your goal is to sort your objects, you can do this with Arrays.sort() by providing a comparator object - see the documentation.



来源:https://stackoverflow.com/questions/22189534/why-are-the-array-indexes-not-being-swapped

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!