Results 1 to 5 of 5
  1. #1
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default need some help with an array

    i am trying to figure out how to set every slot in an array
    here's an example:
    i have an array called arr[]
    there are ten spots in the list
    and i want to set each spot as a
    number starting with 0 and increasing one everytime
    so it will have to be a loop that sets each spot in the array increasing one number every time

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    yea you're right, a loop will the most logical way of doing this. Here's a brief tutorial on it: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    "Experience is what you get when you don't get what you want" (Dan Stanford)
    "Rise and rise again until lambs become lions" (Robin Hood)

  3. #3
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    thanks but the thing is i want to know if there is a way to use a loop to put a number in each slot if it maybe has 200 slots so i dont have to wriet out each number because i already know that way

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    with a for loop, the index variable (often it is "i") is visible inside of the loop, and so you can simply use that variable for both the array index and for the value in the array. For e.g.,
    Java Code:
    for (int i = 0; i < arr.length; i++) {
      arr[i] = i;
    }

  5. #5
    mustachMan is offline Member
    Join Date
    Dec 2009
    Posts
    33
    Rep Power
    0

    Default

    thanks i thought it was going to be something like that using the index variable and i did try that i just put it in the wrong place thanks for the help

Similar Threads

  1. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 11:41 AM
  2. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  3. How to transfer 1D array in JAVA to 3D array in C
    By fishwater00 in forum New To Java
    Replies: 0
    Last Post: 07-31-2009, 06:24 PM
  4. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  5. String array to byte array?!
    By Joe2003 in forum Advanced Java
    Replies: 5
    Last Post: 02-28-2009, 06:09 AM

Posting Permissions

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