1

I'm trying to print a histogram, but am having trouble piecing it all together in main. I'm new to arrays, so if anyone can help with this, it'd be much appreciated. Here are my methods:

public static void main(String[] args) {
  randomIntArray(5);
}

public static int randomInt(int low, int high){
  int x= (int)(Math.random ()*high)+low;
  return x;       
}

public static int[] randomIntArray(int n){
  int[] a = new int [n];
  for (int i = 0;i<a.length;i++){
    a[i]=randomInt (0,100);
  }

  System.out.println(printHist(a));
  return a;
}

public static int[] printHist(int[]a){
  int[] k = new int[11];
  int i=0;
  while (i<=10) { 
    int counter = 0;
    int h=0;
    while(h<a.length) {
      if (a[h] == i) {
        counter++;
        h++;
      }
      h++;
    }

    k[i] = counter;
    i++;
  }

  return k;
}

And here's what I get as output.

[I@fb53f6

Do I need to rethink the way I'm doing this, or is there a simple fix?

t0mppa
  • 3,983
  • 5
  • 37
  • 48
Nick Gatti
  • 73
  • 1
  • 6

1 Answers1

0

System.out.println(arrayObject) does not do what you think it does.

Try one of the solutions at this related question: What's the simplest way to print a Java array? - such as Arrays.toString(arrayObject)

Community
  • 1
  • 1
Krease
  • 15,805
  • 8
  • 54
  • 86
  • Okay, it's somewhat better, but now I'm getting '[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]' – Nick Gatti Nov 24 '13 at 18:09
  • Based on reading your code above, that sounds like `k[i] = counter` is always assigning a `0` value - possibly because either your inner while condition `h – Krease Nov 24 '13 at 18:14