I have an array of four stacks. And I am initializing it using foreach loop.
Stack s[]=new Stack[4];
for(Stack n:s)
{
n=new Stack();
}
System.out.println(s[0]);
But the problem with this is it prints null. But when I replaced this with normal for loop
Stack s[]=new Stack[4];
for(int i=0;i<4;i++)
{
s[i]=new Stack();
}
System.out.println(s[0]);
it prints []. I tried it with other java collections LinkedList,ArrayList etc but all are having same behaviour. My question is why the first method is not working, it used to work everywhere ?