Results 1 to 8 of 8
- 03-01-2011, 02:27 AM #1
- 03-01-2011, 02:32 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 03-01-2011, 02:35 AM #3
how would the naming work. Can you give me an example based on my loop?
- 03-01-2011, 02:36 AM #4
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);
- 03-01-2011, 03:02 AM #5
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++){
}
}
- 03-01-2011, 03:05 AM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 118
- Rep Power
- 0
- 03-01-2011, 03:13 AM #7
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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
so to find the size of the 2d array it would be arr.length.Java Code:array[] arr = new array[3]; arr[0] = new int[3]; arr[1] = new int[3]; arr[2] = new int[3];
In your matrix example you would do
You can easily test this by opening notepad and creating the following programJava Code:int[][] someMatrix = new int[3][3]; for(int i = 0; i < someMatrix.length; i++)
compile this and run it and see what happens.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); } } }Last edited by sunde887; 03-01-2011 at 03:16 AM.
- 03-01-2011, 03:37 AM #8
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
-
Making a loop and creating variables based off of user input
By seanfmglobal in forum New To JavaReplies: 2Last Post: 01-13-2011, 05:43 AM -
Help with creating a while loop!
By BAD in forum New To JavaReplies: 1Last Post: 07-09-2010, 09:00 PM -
Creating an arbitrary number of variables
By theodorekon in forum New To JavaReplies: 1Last Post: 04-15-2010, 06:10 PM -
Trouble with For loop and variables in a program
By dablyz in forum New To JavaReplies: 12Last Post: 05-06-2008, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks