Results 1 to 5 of 5
  1. #1
    seanfmglobal is offline Member
    Join Date
    Nov 2010
    Posts
    40
    Rep Power
    0

    Default Figuring out for????

    I only seem to be replacing the first position of the array when doing this?
    I'm used to using the other for loop (the one below)
    Java Code:
    for(int index = 0; index < array.length; index++)
         code here


    Java Code:
    Random ran = new Random();
    	    int[] numbers = new int[8];
    	    numbers[0]=0;
    	    numbers[1]=0;
    	    numbers[2]=0;
    	    numbers[3]=0;
    	    numbers[4]=0;
    	    numbers[5]=0;
    	    numbers[6]=0;
    	    numbers[7]=0;
    	    
    	    
    	    for(int i : numbers){
    	    	numbers[i] = ran.nextInt(30)+1;		//This is the problem right here!!!!
    	    }
    	   	    
    	    
    	    
    	    
    	    for(int i : numbers)
    	    	System.out.println(i);
    
    	    
    	    
    	}
    But printing all elements in the array seems to be working flawlessly....

    i've used
    Java Code:
    i = ran.nextInt(30);
    Which doesn't change the value of any element
    &
    Java Code:
    numbers[i] = ran.nextInt(30);
    Which changes the value of only the first element


    Thank you for all your help in advanced.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default

    You're using the "enhanced for loop" incorrectly. It simply gives you what's already in the array, you can't use it to change the primitive values. It doesn't give you the index, it gives you the value. And since every value is zero, and you're using that to set the value at that index, you're only setting the first value.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    use the logic....

    int j=0;

    for(int i : numbers)
    {

  4. #4
    Join Date
    Jul 2011
    Posts
    11
    Rep Power
    0

    Default

    int j=0;

    for(int i : numbers)
    {
    numbers[j] = ran.nextInt(30)+1;
    j++;
    }

    here u can change the index .....

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Quote Originally Posted by kalyankasyapcse View Post
    int j=0;

    for(int i : numbers)
    {
    numbers[j] = ran.nextInt(30)+1;
    j++;
    }

    here u can change the index .....
    The above is bad advice; if you want to do that(have access to the indices along the way), don't use an enhanced for loop; but instead use a regular loop. You also want to be careful when making modifications in an enhanced for loop, you may just run into "ConcurrentModificationException", which is caused by fail fast iterators; which the enhanced for uses.

    Java Code:
    for(int j=0;j<arr.length;++j){
      //make changes
    }
    Is the way to go.

    Also, it's unnecessary to initialize each element individiaully to 0; Just declare the array, then loop through and fill it with values.

Similar Threads

  1. Help with figuring out the output
    By Yakg in forum New To Java
    Replies: 3
    Last Post: 12-13-2010, 09:32 AM
  2. Replies: 3
    Last Post: 06-10-2010, 03:42 PM
  3. figuring out the current colour of my appelet
    By vortexprogramming in forum New To Java
    Replies: 2
    Last Post: 07-15-2008, 04:16 AM

Tags for this Thread

Posting Permissions

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