• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
}
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic