Thursday, 29 August 2013

Printing out contents of an array?

Printing out contents of an array?

I have an issue with my java program. I created an array of integers using
my SelectionSortArray class. The issue I am having is that when I try to
print out the contents of my created array it displays some other random
lines of codes which is obviously an error. Below is my work so far. If
you guys can copy and paste it and run it yourselves and tell what is
wrong I will appreciate. Again it does not print the contents from my
array when I run my demo/main.
The end result should print out this:
10
20
30
My demo/main:
public static void main(String[] args) {
SelectionSortArray[] ints = new SelectionSortArray[3];
ints [0] = new SelectionSortArray(10);
ints [1] = new SelectionSortArray(20);
ints [2] = new SelectionSortArray(30);
for (int index = 0; index < ints.length; index++) {
System.out.println(ints[index]);
}
}
My class that I'm using to create the array:
public class SelectionSortArray implements Comparable<SelectionSortArray> {
public int num;
public SelectionSortArray(int initialNum) {
num = initialNum;
}
public int compareTo(SelectionSortArray other) {
int result;
if (num == other.num) {
result = 0;
} else if (num < other.num) {
result = 1;
} else {
result = 2;
}
return result;
}
}

No comments:

Post a Comment