Reputation: 49
This is what I have so far:
import java.util.*;
public class SArray {
private int[] array;
public SArray(int a[]) {
this.array = a;
}
public String toString() {
String arrayString = "";
int i = 0;
while (i < array.length) {
arrayString = arrayString + array[i];
i++;
}
return arrayString;
}
public static void main(String[] args) {
SArray tester = new SArray(new int[] { 23, 17, 5, 90, 12, 44, 38, 84,
77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49 });
tester.toString();
}
}
I looked up how to pass arrays through a constructor and this is what I came up with but no values actually go into the array and Im wondering why?
Upvotes: 1
Views: 56
Reputation: 85781
The values are going into the array but you're doing nothing with them. Probably you want/need to display the values, so use System.out.println
public static void main(String[] args) {
SArray tester = new SArray(new int[] {23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49});
//now you're doing something with the value of toString
System.out.println(tester);
}
Upvotes: 2
Reputation: 2667
The array is there... look:
public static void main(String[] args) {
NewClass tester = new NewClass(new int[]{23, 17, 5, 90, 12, 44, 38, 84,
77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49});
for(int i = 0; i < tester.array.length; i++){
System.out.println(tester.array[i]);
}
}
this is the output:
23, 17, 5, 90, 12, 44, 38, 84, 77, 3, 66, 55, 1, 19, 37, 88, 8, 97, 25, 50, 75, 61, 49,
Upvotes: 1