Doing my final intro to java project. Still pretty new to Java (3 months). I'm crating two arrays and two arraylists of circles. Then i'm sorting one of the lists and one of the arraylists. I'm struggling to figure out how to sort them in ascending order by radius. Using Collections.sort(sortedList) and intellisense is saying i have an error "The method sort(List) in the type Collections is not applicable for the arguments (ArrayList)". Also trying to sort the array as well and the error I'm getting is "The left-hand side of an assignment must be a variable" What am i doing wrong? Is this even the best way to approach the issue? Here's a snippet of my code.
public class CircleList
{
Random rand = new Random();
private Circle[] array = null;
private Circle[] sortedArray = null;
public ArrayList<Circle> list = new ArrayList<Circle>();
public ArrayList<Circle> sortedList = new ArrayList<Circle>();
/**
* Constructor creates Array and List
*
* @param max max size of list
*/
public CircleList(int max)
{
int maxSize = max;
int listSize = rand.nextInt(maxSize) 1;
System.out.println("List size: " listSize);
for (int i = 0; i < listSize; i )
{
list.add(new Circle(rand.nextInt(100) 1));
}
int arraySize = rand.nextInt(maxSize) 1;
System.out.println("Array size: " arraySize);
array = new Circle[arraySize];
sortedArray = new Circle[arraySize];
for (int i = 0; i < arraySize; i )
{
array[i] = new Circle(rand.nextInt(100) 1);
sortedArray[i] = array[i];
}
}
public void getList()
{
for (int i = 0; i < list.size(); i )
{
System.out.println("Radius of circle " i " in the list: " list.get(i).getRadius());
}
}
public void getArray()
{
for (int i = 0; i < array.length; i )
System.out.println("Radius of circle " i " in the array :" array[i].getRadius());
}
public void setSortedList()
{
Collections.sort(sortedList);
}
public void getSortedArray()
{
for (int i = 0; i < sortedArray.length; i )
System.out.println("Radius of circle " i " in the sorted array :" sortedArray[i].getRadius());
}
private void sortArray() {
int startScan =0;
int i =0;
int minI = 0;
double minRadius = 0;
for (startScan =0; startScan < (sortedArray.length -1); startScan ) {
minI = startScan;
minRadius = sortedArray[startScan].getRadius();
for(i = startScan 1; i< sortedArray.length;i ) {
if (sortedArray[i].getRadius() < minRadius) {
minRadius = sortedArray[i].getRadius();
minI = i;
}
}
sortedArray[minI].getRadius()=sortedArray[i].getRadius() ;
minRadius = sortedArray[startScan].getRadius();
}
}
I tried using collections but I don't believe I'm reaching the radius, hence why the error but I may be wrong. When trying to sort the array I feel a tad lost as to why the it's not working.
CodePudding user response:
It looks like you were trying to implement Selection sort algorithm in the sortArray()
method.
If my assumption is correct, then during each iteration of the inner for
-loop, you need to find the Circle
having the smallest radius that reside somewhere in between startScan 1
and the end of the array. And then you need to swap this element (i.e. element at position minI
) with the element at index startScan
.
That how it might be implemented.
private void sortArray() {
int minI;
double minRadius;
for (int startScan = 0; startScan < sortedArray.length - 1; startScan ) {
minI = startScan;
minRadius = sortedArray[startScan].getRadius();
for (int i = startScan 1; i < sortedArray.length; i ) {
if (sortedArray[i].getRadius() < minRadius) {
minRadius = sortedArray[i].getRadius();
minI = i;
}
}
CircleList.Circle temp = sortedArray[startScan];
sortedArray[startScan] = sortedArray[minI];
sortedArray[minI] = temp;
}
}
Note:
There's a built-in functionality for sorting both
List
s and array. But I'm not sure if your assignment allows using it (check your requirements). Since Java 9 we have instance methodList.sort()
(andCollections.sort()
is available from the very early versions of Java). And utility classArrays
offers a group of overloaded methodssort()
which you can use to sort an array of any type.In order to use
List.sort()
orArrays.sort()
with a list or array ofCircle
instances, classCircle
should either implementComparable
interface, or you need to provide aComparator
(for more detail refer to the official tutorial provided by Oracle).