Results 1 to 6 of 6
- 09-15-2012, 03:31 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Static array outside the main method with variable indexes.
Alright so I'm trying to have an array keep its values throughout the class and I'm not sure if there's a better way of doing this than a static array but that is what I know. So I have the array declared and initialized like this:
Now when I try to populate the array in the main method (after I have assigned different values to the x and y values) it doesn't change the indexes in the array because it's already been initialized outside the main method. Now I was wondering if there is a way around this or a better method of having an array keep it's values throughout the entire class and not just one method?Java Code:static int x = 0; static int y = 0; static int matrix[][] = new int [x][y]; public static void main (String args[]) {
- 09-15-2012, 06:14 AM #2
Re: Static array outside the main method with variable indexes.
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 09-15-2012, 09:41 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: Static array outside the main method with variable indexes.
wait so are you asking to change the array size or just the values in the array?
the values in the array should change fine. Something like this
Java Code:import java.util.Random; public class ExamTest { static int x = 7; static int y = 8; static int matrix[][] = new int [x][y]; public static void main (String args[]){ Random ran = new Random(); for(int i=0;i<y;i++){ for(int b=0;b<x;b++){ matrix[b][i] = ran.nextInt(10); System.out.print(matrix[b][i] + ", "); } System.out.print("\n"); } } }
- 09-15-2012, 09:56 AM #4
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Re: Static array outside the main method with variable indexes.
or if you wanted to resize the array you could do something weird like this. Keep in mind that method below could crash if you shrunk the array size so might want to put a if statement or try blocks to make sure something bad doesn't happen.
Java Code:public class ExamTest { static int x = 7; static int y = 8; static int matrix[][] = new int [x][y]; public static void main (String args[]){ matrix = arraySizeIncreaser(matrix, 20, 40); matrix[19][39] = 10; System.out.println(matrix[19][39]); } public static int[][] arraySizeIncreaser(int[][] array, int newArrayX, int newArrayY){ int[][] ret = new int[newArrayX][newArrayY]; for(int x=0;x<array.length;x++){ for(int y=0;y<array[0].length;y++){ ret[x][y] = array[x][y]; } } return ret; } }
- 09-17-2012, 12:30 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 8
- Rep Power
- 0
Re: Static array outside the main method with variable indexes.
Ah yes that's exactly what I was asking for! Thank you so much for this. :)
- 09-17-2012, 06:47 AM #6
Member
- Join Date
- Sep 2012
- Posts
- 70
- Rep Power
- 0
Similar Threads
-
error: Missing method, body, or declare abstract public static void main
By MBD in forum New To JavaReplies: 2Last Post: 09-27-2011, 03:59 PM -
How can you use a variable in a method that is declared in main?
By drhinri in forum New To JavaReplies: 3Last Post: 02-11-2011, 02:40 AM -
Why did Java designers make main method as static ?
By makpandian in forum New To JavaReplies: 9Last Post: 11-13-2010, 09:51 PM -
Java class HashIt with a static recursive method and a static iterative method
By kezkez in forum New To JavaReplies: 3Last Post: 02-09-2010, 05:22 AM -
Same Behavior of VB's Method's Static Variable
By erosszz_jp@yahoo.co.jp in forum New To JavaReplies: 6Last Post: 10-30-2008, 04:54 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks