-1

So here I am again with another problem. You'll understand what i mean as soon as you read my code and output. I'm trying to print my Array's numbers and their occurrences. !!!I can't use any of import codes unfortunately...!!!

        int[] fr = new int[Bag.length];
        for( int i = 0; i < Bag.length; i++ ) 
        fr[i]++; 
        for( int i = 0; i < fr.length; i++ ) { 
        if( fr[i] > 0 ) 
        System.out.println( "The number " + Bag[i] + " occurs " + fr[i] + " time(s).");}}

If i add 5, 6 and 5 to my array; ( I created my Add operation btw so at first my array is 0 and then getting bigger )

Output : The number 5 occurs 1 time(s). The number 6 occurs 1 time(s). The number 5 occurs 1 time(s).

My Add operation btw is :

        int[] Bag = new int[0]; 
        String name = scanner.next();

        if (name.equals("A")){
        int number = scanner.nextInt();    
        NewBag[NewBag.length - 1] = number;
        for(int i = 0; i < Bag.length; i++){
        NewBag[i] = Bag[i];}
        Bag = NewBag;
        System.out.println(number + " added to Bag.");}
Hmerac
  • 187
  • 1
  • 15
  • 1
    Can you post the code in which you add 5, 6 and 5? Maybe it helps to explain where it goes wrong. – Chronio Oct 05 '14 at 18:03
  • This looks like a "histogram" - maybe it helps to google for "histogram". There is a good question/answer on this in SO ( http://stackoverflow.com/questions/13106906/how-to-create-a-histogram-in-java ) – Matthias Oct 05 '14 at 18:05
  • What makes you believe we'll understand when we read your code? As a wild guess I think you want to increment fr[Bag[i]] rather than fr[i]. But that will produce ArrayIndexOutOfBoundsExceptions... – Simon Fischer Oct 05 '14 at 18:07
  • Yes your wild guess is true. – Hmerac Oct 05 '14 at 18:17

3 Answers3

1

Better use Map for this.

Map<Integer,Integer> occurences= new HashMap<Intege,Integer>();
for( int bagValue: bag ){
 int counter = occurences.hasValue(bagValue)? occurences.get(bagValue): 0;
 occurences.put(bagValue, counter++)
}

Will be more agile for this task, and less demanding in memory.

Beri
  • 11,470
  • 4
  • 35
  • 57
  • |Remember that values of your bag can be greater than bah length. Storing all in an array required you to know the max value. – Beri Oct 05 '14 at 18:06
0

Although you have other problems, here is what you should do.

int[] fr = new int[10000];// modified
for( int i = 0; i < Bag.length; i++ ) 
    fr[Bag[i]]++;   // Here
for( int i = 0; i < fr.length; i++ ) { 
    if( fr[i] > 0 ) 
        System.out.println( "The number " + Bag[i] + " occurs " + fr[i] + " time(s).");}}

The problem what is the maximum and minimum number in Bag array? Say Bag=new int[10] and its range is 0-10000; then fr=new int[10001];

Ok, on the other problem, I saw you were trying to increase the size of Bag

    int[] Bag = new int[0]; 
    String name = scanner.next();
    int[] NewBag;
    if (name.equals("A")){
        int number = scanner.nextInt();    
        NewBag = new int[Bag.length+1];// resize the array
        NewBag[NewBag.length - 1] = number;
        for(int i = 0; i < Bag.length; i++){
            NewBag[i] = Bag[i];
        }
        Bag = NewBag;
        System.out.println(number + " added to Bag.");
    }
Hai Bi
  • 1,173
  • 1
  • 11
  • 21
  • Yes it gives me an ArrayIndexOutOfBoundsException error. But i don't know how to do add operation without making array's length 0. – Hmerac Oct 05 '14 at 18:09
  • @HalilM, I modified the first line so you are going to have a fixed size of fr just temporarily for you to get the coding going. – Hai Bi Oct 05 '14 at 18:17
  • Umm I just modified it to 10000. Same error again. I think it'll give me this error as long as i keep my Bag array's length 0, right ? – Hmerac Oct 05 '14 at 18:21
  • ok, I have modified one line of your code to dynamically change the size of NewBag – Hai Bi Oct 05 '14 at 18:25
0

If I understand correctly, you have a superset of ints, and you want to know for each int it's number of occurrences.

For that, you would have to iterate your array, and check for each new int, if it has already been inserted to the array, and if so, increment respectively.

int flag = 1
for (i=0; i<Bag.length;i++){
    for(j=0;j<fr.length;j++){
        if(Bag[i] == fr[j]){
            fr[j]++;
            flag = 0;
            break;
        }
    if(flag){
        fr[i}++;
    }
}
CIsForCookies
  • 12,097
  • 11
  • 59
  • 124