GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Arrays" Watch "Arrays" New topic
Author

Arrays

Shah Chunky
Ranch Hand

Joined: Dec 27, 2000
Posts: 116
Why does Loop at line 2 prints all 0's even if i have assigned the array 'a' at line 1.

class ArrArgs
{
public static void main(String[] args)
{
ArrArgs aa = new ArrArgs(10);
int w[] = aa.reverse(aa.arr);
}

private int[] arr;
ArrArgs(int size) // Constructor
{
arr = new int[size];
}

public int[] reverse(int[] arr)
{
int[] a = new int[10];
for(int i = 0; i < arr.length; i++) <br /> {<br /> arr[i] = i;<br /> }<br /> <br /> for(int i = arr.length-1; i >= 0 ; i--)
{
for (int j=0;j<10;j++)<br /> {<br /> a[j] = arr[i]; // Line 1<br /> }<br /> System.out.println("a[" + i + "]" + "= " + a[i]);<br /> } <br /> System.out.println();<br /> <br /> for(int j = a.length-1; j >= 0 ; j--) // Line 2
{
System.out.println("a[" + j + "]" + a[j]);
}
return arr;
}
}


Shah Chunky - Sun Certified Java2 Programmer.
nan s
Greenhorn

Joined: Dec 28, 2000
Posts: 16
That's right,
i mean:
first; program set arr[0] = 0
then; program set a[j] = 10
then set a[j] =9
then set a[j] =8
....
at last set a[j] = 0
that's why print all '0'
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays