Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays

 
Shah Chunky
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
}
}
 
nan s
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic