I need to find the largest number in array bb and swap it with the first item. I was able to find the largest number and move it to the front, but I can’t get the item in the first position to swap. Any help would be greatly appreciated!
JavaScript
x
import java.util.*;
public class arrayTest
{
public static void main (String[] args)
{
int bb[] = { -9, 55, 10002, -222, 45, -1, 926, 100, -100 };
int i, temp;
int bbLen = bb.length;
System.out.println ("Before: " + Arrays.toString(bb));
int large = bb[0];
for (i = 0; i < bbLen; i++)
{
if (large < bb[i])
large = bb[i];
}
temp = large;
large = bb[0];
bb[0] = temp;
System.out.println ("After: " + Arrays.toString(bb));
}
}
Advertisement
Answer
you have to keep the position and the value of the largest number.
When you have it :
JavaScript
bb[index_bigger] = bb[0];
bb[0] = larger;
Hope help you