• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Output Not understand!!!

 
babul bansal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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 ]
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
babul bansal
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic