I was reading about arrays in java and I made a code for calculating the number of appearances of all numbers in an array .
public class Example {
static int b[] = new int[13]; // I can not do static int b[] = new int[a.length] because a[] in not static array
static int count = 0;
public static void main(String[] args) {
int[] a = { 2, 3, 4, 3, 3, 5, 4, 10, 9, 1, 9, 11, 15 };
counting(a);
printCount();
}
private static void printCount() {
int k = 0;
for (int i = 0; i < b.length; i++) {
System.out.print("number" + " " + a[k] + " " + "is found" + " "); // here I get error in a[k] because it is not static , eclipse says : a cannot be resolved to a variable
System.out.println(b[i] + " " + "times");
k++;
}
System.out.println();
}
private static void counting(int[] a) {
for (int i = 0; i < a.length; i++) {
for (int k = 0; k < a.length; k++) {
if (a[i] == a[k]) {
b[i] = ++count;
}
}
count = 0;
}
}
}
I got stuck in my printCount() method , there I can not call my a[] array in the method because a[] is not static in the main method .
I tried to write static int[] a = { 2, 3, 4, 3, 3, 5, 4, 10, 9, 1, 9, 11, 15 }; in my main method but eclipse does not accept that .
How can I make a[] a static array so that can be reached in all methods in my Example class above ?
Thank you
bandcount. Or you could send the array as a parameter to the method withprintCount(a);and change the signature of the method toprivate static void printCount(int[] a).countingbut you are stuck onprintCount.