Reputation: 9
i've got an array with n element and i'm trying to set the values my array, so that each element has its value as Positon.
I.e. the first element at position 0 is 0, the second element is at position 1 to 1, etc., up to the n-th element at position n-1 with the value of n-1.
And finally i will give the contents of my array on the console.
Well i already set the values correct i think, but i can't show on the console.For example how i can show the the position "n-1" has the value "n-1" ?
Here what i did so far:
public void exercise1(Integer n){
int[] arrayA = new int[n];
int counter;
for(counter=0; counter<arrayA.length; counter++){
arrayA[counter]=counter;
}
}
Thanks in advance:)
Upvotes: 0
Views: 5203
Reputation: 9
public class apples{
public static void main(String[] args) {
exercise(4);
}
public void exercise(Integer n){
int[] arrayA = new int[n];
int counter;
for(counter=0; counter<arrayA.length; counter++){
arrayA[counter]=counter;
}
System.out.print("[");
for(counter=0; counter<arrayA.length; counter++){
if(counter == n-1){
System.out.print(arrayA[counter]);
}else{
System.out.print(arrayA[counter] + ", ");
}
}
System.out.println("]");
}
}
Well guys, i did something like that after your advicesand it works. Thank you very much:)
Upvotes: 0
Reputation: 472
i think what Hélio Santos says it's true and it's works for me :
public class koponk {
public static void main(String[] args) {
exercise1(10);
}
public static void exercise1(Integer n) {
int[] arrayA = new int[n];
int counter;
for (counter = 0; counter < arrayA.length; counter++) {
arrayA[counter] = counter;
System.out.print(arrayA[counter] + " ");
}
}
}
Upvotes: 0
Reputation: 3255
Use System.out.println and this code:
System.out.println(Arrays. toString(your_array_name_here));
Upvotes: 1