-6

Please could you tell me how its work in section (g[ss[i]]++;) and tell me the sequence of output in java

class A{
    public static void main(String []a){
        int []ss={1,2,3,4,2,3,3,1,1,1,5,6,4,5,4};
        int []g=new int[15];
        for(int i=0;i<15;i++){
            g[ss[i]]++;
        }
        for(int i=1;i<15;i++){
            System.out.println(ss[i-1]+"=="+g[i]);
        }

    }
}
4
  • Have you tried running it, that will give you the output Commented Nov 28, 2013 at 16:26
  • 2
    Please could you attempt to write this in a more complicated way? Commented Nov 28, 2013 at 16:28
  • @RossDrew All this just to print out a series of lies 1==4 indeed Commented Nov 28, 2013 at 16:32
  • @Richard Tingle: I guess it should be "1 was found 4 times". There are a couple of flaws with the code, such as the hard coded 15. Commented Nov 28, 2013 at 16:33

2 Answers 2

1

Can't you run it?

g[ss[i]]++; can be rewritten as

int index = ss[i];
g[index] = g[index] + 1;

So it's counted number of each number in ss.

It's very error prone, and you should never do something like that.

2
  • please could you explain more about g[ss[i]]++; ? Commented Nov 30, 2013 at 11:13
  • @user What exactly is unclear? I'v described operation order. One more, maybe a little clearer way may be g[ss[i]] = g[ss[i]] + 1 Commented Nov 30, 2013 at 15:13
0

Just run it?

1==4
2==2
3==3
4==3
2==2
3==1
3==0
1==0
1==0
1==0
5==0
6==0
4==0
5==0

This should be your output.

2
  • thanks for answering . yeah i know how to run it but i just wanted to know how this line worked g[ss[i]]++; @seagull Commented Nov 29, 2013 at 11:52
  • thanks for answering . yeah i know how to run it but i just wanted to know how this line worked g[ss[i]]++; @leviathan Commented Nov 29, 2013 at 11:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.