|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-21-2008, 03:02 PM
|
 |
Member
|
|
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
|
|
|
[SOLVED] Arrays/for-loops (easy) need help.
Hey I am suppose to rewrite the following code but make it so that the array is initialized on one line of code and a loop is used to display for the results.
class OneDimensionArray {
public static void main(String args[]) {
// Declare and allocate space
int myarray[] = new int[4];
// Initialize elements
myarray[0] = 33;
myarray[1] = 71;
myarray[2] = -16;
myarray[3] = 45;
// Display length
System.out.println("myarray.length = " +
myarray.length);
// Display elements
System.out.println(myarray[0]);
System.out.println(myarray[1]);
System.out.println(myarray[2]);
System.out.println(myarray[3]);
}
}
__________________
I am a Java n00b.
|
|

04-21-2008, 03:14 PM
|
|
Moderator
|
|
Join Date: Nov 2007
Posts: 1,657
|
|
|
It is easy. Check array initialization part of Java tutorial again and use one println statement inside a for loop where array index is replaced with loop control variable!
__________________
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on July 27, 2008)
|
|

04-21-2008, 07:31 PM
|
 |
Moderator
|
|
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
|
|
Originally Posted by Java Tip
It is easy. Check array initialization part of Java tutorial again and use one println statement inside a for loop where array index is replaced with loop control variable!
Well said!
Zebra, should this solve your answer, please mark this thread as solved- if not, post back your updated code and we'll assist you.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. !
Got a little Capt'n in you? (drink responsibly)
|
|

04-22-2008, 08:27 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,574
|
|
Yep, don't have any feedback from Zebra on what JavaTip and Captain says. It's not a difficult one. JavaTip gives all the details there. 
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-23-2008, 02:52 PM
|
 |
Member
|
|
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
|
|
|
So where do I put the for-loop. Sorry?
__________________
I am a Java n00b.
|
|

04-23-2008, 03:11 PM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 249
|
|
hello zebra,
As you have told that you are fresher, then my advise is to read some core java fundamentals books and do some research on programs . If you do something at your own then you will learn more. and one more thing do more and more practice on core java. CORE JAVA IS THE TOUGHEST part in Java language, Once you grab good knowledge on core part you will not get difficulty on other parts like servlets,jsp etc.
well for now i think you require this.
class OneDimensionArray {
public static void main(String args[]) {
// Declare and allocate space
int myarray[] = new int[]{33,71,-16,45};
// Initialize elements
// Display length
System.out.println("myarray.length = " + myarray.length);
// Display elements
for (int i=0;i<myarray.length;i++ ){
System.out.println(myarray[i]);
}
}
}
__________________
sanjeev,संजीव
|
|

04-23-2008, 03:15 PM
|
 |
Member
|
|
Join Date: Apr 2008
Location: Louisville, Indiana/Kentucky
Posts: 64
|
|
|
Thank you very much! I am in a java programming I class and he tries to just teach us, without books and stuff. But, thanks again. I will research.
__________________
I am a Java n00b.
|
|

04-24-2008, 05:03 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 3,574
|
|
You got the answer It's not difficult pal. Read more about and do some experiment yourself.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|