aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Output Not understand!!! 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 "Output Not understand!!!" Watch "Output Not understand!!!" New topic
Author

Output Not understand!!!

babul bansal
Greenhorn

Joined: Aug 11, 2007
Posts: 24
Please tell me how these arrays and for loop are behaving.
There are two programs, every program is followed by its output.

//Prog1
public class we1
{
public static void main(final String[] args)
{
int [] arr = { 1 ,2 ,3 ,4 ,5};
int [] arr2 = new int[1];
arr2 = arr;
for(int a:arr)
System.out.println(arr2[a]);


}
}

OUTPUT:
F:\scjp>java we1
2
3
4
5
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at we1.main(we1.java:10)


//Prog2

public class we1
{
public static void main(final String[] args)
{
int [] arr = { 1 ,2 ,3 ,4 ,5};
int [] arr2 = new int[1];
arr2 = arr;

System.out.println(arr2[4]);


}
}

OUTPUT:5
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

//Prog1

first of all the line at (1) is confusing. The intialization has no meaning. It can also be written as int[] arr2;

Now to the loop. It runs as-
for(int a = arr[0]/*arr[0] is 1*/; a < arr.size(); a++)
{
System.out.println(arr2[a]);
}

When you execute it will get these values-
arr2[1] -->2
arr2[2] -->3
arr2[3] -->4
arr2[4] -->5
arr2[5] --> ArrayIndexOutOfBounds

Actually in this program both arr and arr2 refer to the same array. the for loop will traverse all the elements in the arr (which are 1,2,3,4,5) and print the value stored at the corresponding index in arr2. I hope you will get it. :roll:

[ August 06, 2008: Message edited by: AnkitJi Garg ]
[ August 06, 2008: Message edited by: AnkitJi Garg ]

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

//Prog2

This one is pretty simple. Both arr2 and arr refer to the same array. so arr2 has this array-
arr2[0]-->1
arr2[1]-->2
arr2[2]-->3
arr2[3]-->4
arr2[4]-->5

so when you access the value of arr2[4], 5 is printed.
Mario Razec
Greenhorn

Joined: Aug 01, 2008
Posts: 17
hi babul,

Checking for each: http://www.leepoint.net/notes-java/flow/loops/foreach.html

Prog2
Remember Array: "Each item in an array is called an element, and each element is accessed by its numerical index"

this Line: System.out.println(arr2[4]);
you accessed 4 element of array:

Index : 0 1 2 3 4
Element: {1, 2, 3, 4, 5}

So output: 5

Details checking: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html

Cya brother..
I hope that helps!
[ August 06, 2008: Message edited by: Razec ]
babul bansal
Greenhorn

Joined: Aug 11, 2007
Posts: 24
Thanks for the replies guys, but in both the programs I just did not get one thing, that arr2 size is just 1, so how it can take a whole array with 5 elements or length 5 or how we can access the 4th element of arr2.
I understand this thing that arr2=arr is a reference, means arrays are objects, but still does not arrays length matter?

Please explain. Waiting for the reply......
Meena Ajay
Ranch Hand

Joined: May 28, 2008
Posts: 38

Hi,
Even though originally arr2 was constructed as array with single element, now the reference is reassigned in step arr2= arr.
So it starts referring to the object with 5 elements i.e., Both arr and arr2 are references to the same array object with 5 elements {1,2,3,4,5}.
Hope this helps.


Cheers, Meena
OCPJP 6
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8813
    
    5
Hey babul,

What "exactly" are "arr" and "arr2" - be more specific than just that "they are references". Then, once you have answered that question, ask yourself (or answer on this thread), are these so-called references mutable? If so, what does that mean?

hth,

Bert

p.s. The idea here is that you must become 100% clear on how references - and the objects they refer to - interact.


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
babul bansal
Greenhorn

Joined: Aug 11, 2007
Posts: 24
After reading Bert's post, I enquired on this link,

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html
as given to me by Razec.

"An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed."


But after Meena post, at least I can just judge that what is happening. Anything if any one wants to add.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Output Not understand!!!