Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

need some help with arrays

843789Jul 12 2009 — edited Jul 16 2009
I'm learning about arrays, and the book I've got is confusing about creating/assigning values to them. I know how to declare a variable for an array, but when it comes to assigning (initializing?) the elements in the array, I've run into a problem in my little practice program.

I tried creating/declaring an array and assigning values to the array in one statement, using brackets:
int[] series={new int(1), new int(2), new int(3)};

		out.println("Element 0: " + series [0]);
		out.println("Element 1: " + series [1]);
		out.println("Element 2: " + series [2]);
When I tried to compile, I got all sorts of error messages:
Array1.java:10: '[' expected
		int[] series={new int(1), new int(2), new int(3);
		                     ^
Array1.java:10: ']' expected
		int[] series={new int(1), new int(2), new int(3);
		                        ^
Array1.java:10: '[' expected
		int[] series={new int(1), new int(2), new int(3);
		                                 ^
Array1.java:10: ']' expected
		int[] series={new int(1), new int(2), new int(3);
		                                    ^
Array1.java:10: '[' expected
		int[] series={new int(1), new int(2), new int(3);
		                                             ^
Array1.java:10: ']' expected
		int[] series={new int(1), new int(2), new int(3);
		                                                ^
Array1.java:10: ';' expected
		int[] series={new int(1), new int(2), new int(3);
		                                                 ^
Array1.java:12: illegal start of expression
		out.println("Element 0: " + series [0]);
		   ^
Array1.java:12: ';' expected
		out.println("Element 0: " + series [0]);
		           ^
Array1.java:12: not a statement
		out.println("Element 0: " + series [0]);
		                                   ^
Array1.java:12: ';' expected
		out.println("Element 0: " + series [0]);
		                                      ^
If I code it this way:
  int[] series=new int[3];
		series [0]=1;
		series [1]=2;
		series [2]=3;
It compiles and runs fine.

I was confused -- the book I've got cites the first way as a way of creating/declaring an array and initializing/assigning a value to each element. Or what am I getting wrong (I'm still a newbie, so I probably am missing something :-).
How does one create/declare a variable for an array and then assign values to each element -- It's not a problem when you have only a few indexes, but what if you have hundreds, or thousands, or even more? And what exactly does my first coding example do? My book says that 'Because each object in an array of objects has a bull reference when created, you must assign an object to each array element before using it.' I thought the use of the parentheses assigned a value to that particular element in the array.

Edited by: Straitsfan on Jul 12, 2009 12:17 PM

Edited by: Straitsfan on Jul 12, 2009 12:19 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 13 2009
Added on Jul 12 2009
16 comments
361 views