MrTimotheos
MrTimotheos

Reputation: 937

Why are the array indexes not being swapped?

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.

Upvotes: 0

Views: 80

Answers (5)

DNA
DNA

Reputation: 42597

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.

Upvotes: 0

Hungry Blue Dev
Hungry Blue Dev

Reputation: 1325

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.

Upvotes: 1

Sagar Gandhi
Sagar Gandhi

Reputation: 965

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

Upvotes: 0

Vishal Santharam
Vishal Santharam

Reputation: 2023

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

Upvotes: 1

Christian Tapia
Christian Tapia

Reputation: 34146

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;

Upvotes: 4

Related Questions