# Sorting a list of Integers

Page 1 of 1

## 8 Replies - 498 Views - Last Post: 06 October 2012 - 09:07 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294562&amp;s=02564b1e115cf3387219c0372ce4f07d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 diagnonsense

Reputation: 0
• Posts: 47
• Joined: 11-February 12

# Sorting a list of Integers

Posted 06 October 2012 - 07:27 PM

Hey, I'm getting a confusing error that I'm not sure how to fix, can anyone help me out?

```import java.util.Scanner;
public class Numbers
{
public static void main (String[] args)
{
int size;
int[] intList;

Scanner scan = new Scanner(System.in);
System.out.print ("\nHow many integers do you want to sort? ");
size = scan.nextInt();

System.out.println ("\nEnter the numbers...");
for (int i = 0; i < size; i++)
intList[i] = scan.nextInt(); //THIS IS WHERE THE ERROR OCCURS

IntegerList intList2 = new IntegerList(size);
System.out.println(intList2);
Integer[] intCopy = intList2.makeIntegers();
Sorting.selectionSort(intCopy);

System.out.println ("\nYour numbers in sorted order...");
for (int i = 0; i < intCopy.length; i++)
System.out.print(intCopy + "  ");
System.out.println ();
}
}
```

```public class IntegerList implements Comparable
{
int[] list; //values in the list

public IntegerList(int size)
{
list = new int[size];
}

public int[] getInts(){
return list;
}

/*Converting int into Integer*/
public Integer[] makeIntegers(){
Integer[] newlist = new Integer[list.length];
for (int i = 0;i < list.length; i++)
newlist[i] = new Integer(list[i]);
return newlist;
}

public void randomize()
{
for (int i=0; i<list.length; i++)
list[i] = (int)(Math.random() * 1000000) + 1;
}

public String toString()
{
String result = "";
for (int i=0; i<list.length; i++)
result += list[i] + "\t";
result += "\n\n";
return result;
}

public double average(){
double sum = 0;
for (int i=0; i<list.length; i++)
sum += list[i];
return sum / list.length;
}

public int compareTo(Object obj){
double myavg = average();
double otheravg = ((IntegerList)obj).average();
if (myavg == otheravg) return 0;
else if (myavg < otheravg) return -1;
else return 1;
}
}

```

In the Numbers.java code I get the error " The local variable intList may not have been initialized" but I thought I initialized it when I wrote "int[] intList"
Is this wrong?

This post has been edited by diagnonsense: 06 October 2012 - 07:30 PM

Is This A Good Question/Topic? 0

## Replies To: Sorting a list of Integers

### #2 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: Sorting a list of Integers

Posted 06 October 2012 - 07:38 PM

Arrays are not dynamic. They cannot grow or shrink. You did not set the amount of numbers you are holding for the array, so Java has no clue how many numbers the array is going to hold. So right before the for loop make initialize the array.

```intList = new int[size];

```

### #3 diagnonsense

Reputation: 0
• Posts: 47
• Joined: 11-February 12

## Re: Sorting a list of Integers

Posted 06 October 2012 - 07:43 PM

Kinaces, on 06 October 2012 - 07:38 PM, said:

Arrays are not dynamic. They cannot grow or shrink. You did not set the amount of numbers you are holding for the array, so Java has no clue how many numbers the array is going to hold. So right before the for loop make initialize the array.

```intList = new int[size];

```

Ahh makes sense, but now I'm getting the "intList cannot be resolved to a variable" error.
I've been looking at this program too long now, it's all looking like a foreign language at this point.

```import java.util.Scanner;
public class Numbers
{
//---------------------------------------------
//  Reads in an array of integers, sorts them,
//  then prints them in sorted order.
//---------------------------------------------
public static void main (String[] args)
{
int size;

Scanner scan = new Scanner(System.in);
System.out.print ("\nHow many integers do you want to sort? ");
size = scan.nextInt();

System.out.println ("\nEnter the numbers...");
intList = new int[size];
for (int i = 0; i < size; i++)
intList[i] = scan.nextInt();

IntegerList intList2 = new IntegerList(size);
System.out.println(intList2);
Integer[] intCopy = intList2.makeIntegers();
Sorting.selectionSort(intCopy);

System.out.println ("\nYour numbers in sorted order...");
for (int i = 0; i < intCopy.length; i++)
System.out.print(intCopy + "\t");
System.out.println ();
}
}
```

### #4 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: Sorting a list of Integers

Posted 06 October 2012 - 07:50 PM

Now you took out

```int[] initList; //setting the type and name.

```

Remember there are two pars of making a variable.
The type and name, and initializing the variable.

You cannot initialize a variable without setting a name and type, and you cannot use a variable without initializing it.

So you need both statements.

This post has been edited by Kinaces: 06 October 2012 - 07:50 PM

### #5 diagnonsense

Reputation: 0
• Posts: 47
• Joined: 11-February 12

## Re: Sorting a list of Integers

Posted 06 October 2012 - 08:06 PM

Kinaces, on 06 October 2012 - 07:50 PM, said:

Now you took out

```int[] initList; //setting the type and name.

```

Remember there are two pars of making a variable.
The type and name, and initializing the variable.

You cannot initialize a variable without setting a name and type, and you cannot use a variable without initializing it.

So you need both statements.

Okay that is resolved.

Now I have to take the integers from intList and copy them to a new array.
I did that and made intCopy, but it will not let me convert intCopy to an array of Integers?

```import java.util.Scanner;
public class Numbers
{
//---------------------------------------------
//  Reads in an array of integers, sorts them,
//  then prints them in sorted order.
//---------------------------------------------
public static void main (String[] args)
{
int size;
int[] intList;

Scanner scan = new Scanner(System.in);
System.out.print ("\nHow many integers do you want to sort? ");
size = scan.nextInt();

System.out.println ("\nEnter the numbers...");
intList = new int[size];
for (int i = 0; i < size; i++)
intList[i] = scan.nextInt();

Integer[] intCopy = new Integer[size];
for (int j = 0; j < intList.length; j++){
intCopy[j] = new Integer(intList[j]);
System.out.println(intCopy[j]);
}

IntegerList intCopy2 = intCopy.makeIntegers(); // IT WANTS ME TO MAKE THIS intCopy.Length
Sorting.selectionSort(intCopy2); // THIS SAYS THAT THERE IS NO COMPARABLE FOR THE VARIABLE INT, I KNOW THIS HENSE WHY I PUT THE LINE BEFORE BUT IT WON'T WORK?

/*System.out.println ("\nYour numbers in sorted order...");
for (int i = 0; i < intCopy.length; i++)
System.out.print(intCopy + "\t");
System.out.println ();*/
}
}

```

### #6 Kinaces

Reputation: 78
• Posts: 230
• Joined: 04-October 12

## Re: Sorting a list of Integers

Posted 06 October 2012 - 08:19 PM

I do not understand why you are trying to make the array into a IntegerList.

then do

Collection.sort((list name))

### #7 diagnonsense

Reputation: 0
• Posts: 47
• Joined: 11-February 12

## Re: Sorting a list of Integers

Posted 06 October 2012 - 08:28 PM

Kinaces, on 06 October 2012 - 08:19 PM, said:

I do not understand why you are trying to make the array into a IntegerList.

then do

Collection.sort((list name))

The only reason I wouldn't do that is because we haven't covered that in my course so I didn't want to put things like that in.

### #8 diagnonsense

Reputation: 0
• Posts: 47
• Joined: 11-February 12

## Re: Sorting a list of Integers

Posted 06 October 2012 - 08:48 PM

I've narrowed down the error to the first for loop

```import java.util.Scanner;
public class Numbers
{
//---------------------------------------------
//  Reads in an array of integers, sorts them,
//  then prints them in sorted order.
//---------------------------------------------
public static void main (String[] args)
{
int size;
//int[] intList;
//int[] intCopy;

Scanner scan = new Scanner(System.in);
System.out.print ("\nHow many integers do you want to sort? ");
size = scan.nextInt();

System.out.println ("\nEnter the numbers...");
IntegerList intList = new IntegerList(size);
for (int i = 0; i < size; i++)
intList[i] = scan.nextInt(); // ERROR

Integer[] intCopy = intList.makeIntegers();
for (int i = 0; i < intCopy.length; i++)
intCopy[i] = new Integer(intCopy[i]);

Sorting.selectionSort(intCopy);

System.out.println ("\nYour numbers in sorted order...");
for (int i = 0; i < intCopy.length; i++)
System.out.print(intCopy[i] + "\t");
System.out.println ();
}
}
```

It outputs the error "The type of the expression must be an array type but it resolved to IntegerList"

### #9 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11241
• Posts: 42,295
• Joined: 27-December 08

## Re: Sorting a list of Integers

Posted 06 October 2012 - 09:07 PM

This is a question about an is-a vs. has-a relationships. IntegerList has-an array, but it isn't an array. Therefore, you cannot use the intList[i] array syntax with IntegerList. Looking at IntegerList, you cannot add elements. Instead, you have to populate an array to pass to it.

Also, Comparable is a generic interface, so you should implement Comparable<IntegerList> to adhere to Java standards.