-4

I am using Intellj Idea and I am playing with ArrayLists, Arrays and a sorting algorithm I want to generate 5 random grades put them into an ArrayList convert the values of the ArrayList into an Array. Sort the Array values from least to greatest and print to screen the grades before and after sort here is what I have:

    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.Arrays;


    public class Grades {




    public static void main(String args[])
{
    ArrayList <Integer> scores = new ArrayList<Integer>();

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

        scores.add((int) (Math.random() * 30 + 1));
    }
    for (int i =0; i < scores.size();i++)
    {
        System.out.println(scores.get(i));
    }
    int arrayOne[] = new int[scores.size()];
    arrayOne = scores.toArray(new int[scores.size()]);

The last line is where I have the problem. Intellij says: no suitable method found for toArray(int[]).

Is my syntax wrong?

Did I forget to import a library?

Is it possible for ArrayLists and Arrays to interact in this way?

Please help.

    int first,second, temp=0;

    for(first=0; first < arrayOne.length -1; first++)
    {
        for (second = 0; second < arrayOne.length - 1 - first; second++)
        {
            if(arrayOne[second] < arrayOne[second +1])
            {
                temp = arrayOne[second];
                arrayOne[second] = arrayOne[second + 1];
                arrayOne[second+1] = temp;
            }
        }
    }

I haven't been able to run this bit to see if it sorts the way I want it too yet.

    for(int i = 0; i < arrayOne.length; i++)
    {
        System.out.println(arrayOne[i]);
    }


}

}

1 Answers1

0

As you can see from toArray(T[]) contract it takes an array containing a reference type and int is not a reference type.

Change your code as to arrayOne is of type Integer[]

Integer[] arrayOne = scores.toArray(new Integer[scores.size()]);
user7
  • 14,505
  • 3
  • 35
  • 62