Results 1 to 8 of 8
  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default creating variables in a for loop?

    if I have a for loop, how can I create new variables inside the loop (i.e, if I wanted to create a new string each time the loop ran. I dont think I can make the name of the string variable:
    int size = 6;
    for (int i=0; i<size; i++){

    **create a new string**

    }

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

    Default

    You can't. It will live only as long as the closing curly brace.

    You can however declare an array/arraylist before the loop, then add Strings to it each time you go through the loop. Outside of the loop you can assign each item in the array/arraylist to a new string variable.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    how would the naming work. Can you give me an example based on my loop?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    It is not clear what you are trying to do.

    If you are trying to create a String by adding on new data to the old String each time around the loop, then you can declare a variable before the loop and use it inside. By the wa if this what you are doing then use a StringBuffer or StringBuilder instead.
    Java Code:
    StringBuilder sb = new StringBuilder();
    for(int index = 1; index <=10; index++) {
        sb.append(String.valueOf(index);
        sb.append(" ");
    }
    String output = sb.toString().trim();
    System.out.println(output);

  5. #5
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    356
    Rep Power
    5

    Default

    maybe I should take this one step at a time;

    One question:

    if I had a method and I wanted to run a for loop x times, where x is the size of the first array, how would I get the value of x in my for loop,

    public void myMethod(int[x][] matrix){

    for (int i=0; i< i need the size of x; i++){

    }

    }

  6. #6
    NRitH is offline Senior Member
    Join Date
    Feb 2011
    Posts
    118
    Rep Power
    0

    Default

    Quote Originally Posted by sehudson View Post
    maybe I should take this one step at a time;

    One question:

    if I had a method and I wanted to run a for loop x times, where x is the size of the first array, how would I get the value of x in my for loop,

    public void myMethod(int[x][] matrix){

    for (int i=0; i< i need the size of x; i++){

    }

    }
    Either declare a variable to store the length of matrix before the for loop, or inside it:

    for (int i=0, size=matrix.length; i < size; ++i)

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

    Default

    any time you see a 2d array stop thinking of it as such.

    all arrays are items.

    Some arrays hold primitives, some hold objects. Since an array can hold an object, it can hold another array. So when you see a 2d array think of this
    Java Code:
    array[] arr = new array[3];
    arr[0] = new int[3];
    arr[1] = new int[3];
    arr[2] = new int[3];
    so to find the size of the 2d array it would be arr.length.

    In your matrix example you would do
    Java Code:
    int[][] someMatrix = new int[3][3];
    for(int i = 0; i < someMatrix.length; i++)
    You can easily test this by opening notepad and creating the following program
    Java Code:
    public class Test{
      public static void main(String[] args){
        int[][] someMatrix = new int[3][4];
        System.out.println(someMatrix.length);
        for(int i = 0; i < someMatrix.length; i++){
          System.out.println(i + ": " +someMatrix[i].length);
        }
      }
    }
    compile this and run it and see what happens.
    Last edited by sunde887; 03-01-2011 at 03:16 AM.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Another thing. A 2D array does not have to be symmetric. You can have what is known as a jagged array. Expanding upon the above example
    Java Code:
    public class Test{
      public static void main(String[] args){
        int[][] someMatrix = {{1,2,3},{3}, {4,5,6,7}, {3,4}};
        System.out.println(someMatrix.length);
        for(int i = 0; i < someMatrix.length; i++){
          System.out.println(i + ": " +someMatrix[i].length);
        }
      }
    }

Similar Threads

  1. Replies: 2
    Last Post: 01-13-2011, 05:43 AM
  2. Help with creating a while loop!
    By BAD in forum New To Java
    Replies: 1
    Last Post: 07-09-2010, 09:00 PM
  3. Creating an arbitrary number of variables
    By theodorekon in forum New To Java
    Replies: 1
    Last Post: 04-15-2010, 06:10 PM
  4. Trouble with For loop and variables in a program
    By dablyz in forum New To Java
    Replies: 12
    Last Post: 05-06-2008, 04:25 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
  •