Results 1 to 8 of 8
  1. #1
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default [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.

    Java Code:
    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.

  2. #2
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    9

    Default

    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!

  3. #3
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    Quote Originally Posted by Java Tip View Post
    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.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. :)

  5. #5
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    So where do I put the for-loop. Sorry?
    I am a Java n00b.

  6. #6
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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.

    Java Code:
    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,संजीव

  7. #7
    Zebra's Avatar
    Zebra is offline Member
    Join Date
    Apr 2008
    Location
    Louisville, Indiana/Kentucky
    Posts
    64
    Rep Power
    0

    Default

    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.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. Noob question- easy
    By mattonitto in forum New To Java
    Replies: 7
    Last Post: 06-13-2008, 12:26 AM
  2. Some problems with arrays and loops
    By BHCluster in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 12:24 PM
  3. help me with a realy easy program (substring)
    By michcio in forum New To Java
    Replies: 7
    Last Post: 01-27-2008, 12:41 AM
  4. Easy question
    By JavaNoob in forum New To Java
    Replies: 10
    Last Post: 08-03-2007, 10:28 PM
  5. easy way to study the java springs concept
    By kumar84 in forum New To Java
    Replies: 1
    Last Post: 07-17-2007, 03:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •