Solved

Java array

Posted on 2017-04-06
10
56 Views
Last Modified: 2017-04-10
Hello
I hate asking this question because it is so simple, but it does not work.  The way it is shown below does work.
Next I want to initialize and print the array within the for loop, but it does not like the System.out.println(a[i]);

public class ArrayTenElements {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//int i=4;
		int a[]= new int[4];
		a[0]=0;
		a[1]=1;
		a[2]=2;
		a[3]=3;
		
		for(int i=0;i<=3;i++)
		{
		//int a[]= new int[i]; 
		
		System.out.println(a[i]);
		}

		
		/*for(int i=0;i<=9;i++)
		{
			//a[i];
			System.out.println(a[5]);
			//i--;
		}*/
	}

}

Open in new window


What am I missing.  Or is it that it can't be done?
Thanks
0
Comment
Question by:chima
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 35

Expert Comment

by:mccarl
ID: 42082971
A bit hard to understand what's going on here, so let me break a few things down. Firstly the code..

Are you saying that the code above works (which I agree with) but if you uncomment line 14 it fails? If so, then yes, because with line 14 uncommented, what you are asking it to do is...

- Loop around 4 times (i from 0 to 3 inclusive)
  - For each loop, create an array whose size is determined from the index i
  - Print out the ith element from the array just created

The problem is that on the first loop, i = 0 so you create a 0 length array, but then you attempt to print the array element with index 0 (which due to 0 based index is the FIRST element). But there is no first element because it has 0 length, i.e. there are 0 elements in the array, and so it fails on IndexOutOfBounds exception.

Even if it got past this first loop, the second would create an array of size = 1, but then you try to print the a[1] element which is the SECOND element but there is only 1 element, so it still throws an exception.



So, now to what you are trying to do... It not clear from "Next I want to initialize and print the array within the for loop" EXACTLY what you are trying to get it to do. If you can elaborate on this, then we can help further.
1
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 42082975
you are crossing the array bound.
i<=9 is wrong. as your array having only 5 elements.
you should access from 0 to 4.

make i<=4
and in print statement, make it a[i]
System.out.println(a[i]);
0
 
LVL 9

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 42082980
sorry, Ur array having 4 elements. so replace accordingly in my above comment.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:chima
ID: 42083174
Allow me to ask the question this way; why is it that I can't create and initialize the array inside of the for loop?
The commented out section below works (last night I realized I was not assigning a value to the array cells);

public class ArrayTenElements {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//int i=4;
		//int a[]= new int[4];
		//a[0]=0;
		//a[1]=1;
		//a[2]=2;
		//a[3]=3;
		
		/*for(int i=0;i<=3;i++)
		{
		//int a[]= new int[i]; 
		a[0]=0;
		a[1]=1;
		a[2]=2;
		a[3]=3;
		//a[i]=i;
		System.out.println(a[i]);
		}*/
		
		for(int i=0;i<=3;i++)
		{
			int a[]= new int[i];
			a[i]=i;
			System.out.println(a[i]);
		}
	}
}

Open in new window

0
 

Author Comment

by:chima
ID: 42083207
Correction; this works

		for(int i=0;i<=3;i++)
		{
		int a[]= new int[4]; 
		a[i]=i;
		//a[0]=0;
		//a[1]=1;
		//a[2]=2;
		//a[3]=3;
		System.out.println(a[i]);
		}

Open in new window


So int[] was the problem
0
 

Author Comment

by:chima
ID: 42083212
Strange, in my last entry I had this "So XXX was the problem"  outside of the code section, and Expert-Exchange was not allowing me to submit my comment.  And the same here; I have to put it inside the ????! Wow!
0
 

Author Comment

by:chima
ID: 42083215
ling 3 above could not use an "i"  
This code is better;
package Arrays;

public class ArrayTenElements {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int j=10; // 10 elements
				
		for(int i=0;i<=(j-1);i++)
		{
		int a[]= new int[j]; 
		a[i]=i;
		System.out.println(a[i]);
		}
	}
}

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 42083507
As has been said, it isn't clear what you want to do, but what you are doing at the moment simply initialises one element of the array at a time, in step with the loop counter - so you have 9 empty elements, and only ever print out one of them. So that surely can't be what you want ?
0
 
LVL 35

Expert Comment

by:mccarl
ID: 42083607
And the same here; I have to put it inside the ????! Wow!

Yes, if you were trying to say "So int[ i] was the problem", you will run into trouble because "i" inside square brackets is interpreted as a formatting instruction and if you don't have a corresponding end tag, it will complain. The way I get around it is as above, put a space either side of the i

As for the code, like krakatoa said, you need to explain (in English, not code) what it is that you are trying to do.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 42083614
Ooooh, the error message that I just got about the i tag, just told me another way to do it... put a close bracket just after the open to escape it. Let me try...

int a[] = new int[i];

I typed this...
int a[]] = new int[]i];

Open in new window


Cool!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Facing this issue for maven proxy setting 2 27
difference between sorce folder and folder in eclipise 3 41
Programming Language for Wordpress 7 63
batch file or script 4 48
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question