Results 1 to 6 of 6
  1. #1
    musdem is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Exclamation 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:
    Java Code:
    static int x = 0;
    static int y = 0;
    static int matrix[][] = new int [x][y];
    	public static void main (String args[])
    	{
    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?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default 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");
    	    	}
    	    	
    	    }
    
    }

  4. #4
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default 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;
    	    }
    
    }

  5. #5
    musdem is offline Member
    Join Date
    Mar 2012
    Posts
    8
    Rep Power
    0

    Default 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. :)

  6. #6
    killutch is offline Member
    Join Date
    Sep 2012
    Posts
    70
    Rep Power
    0

    Default Re: Static array outside the main method with variable indexes.

    Quote Originally Posted by musdem View Post
    Ah yes that's exactly what I was asking for! Thank you so much for this. :)
    Good, glad I could help.

Similar Threads

  1. Replies: 2
    Last Post: 09-27-2011, 03:59 PM
  2. Replies: 3
    Last Post: 02-11-2011, 02:40 AM
  3. Why did Java designers make main method as static ?
    By makpandian in forum New To Java
    Replies: 9
    Last Post: 11-13-2010, 09:51 PM
  4. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  5. Same Behavior of VB's Method's Static Variable
    By erosszz_jp@yahoo.co.jp in forum New To Java
    Replies: 6
    Last Post: 10-30-2008, 04:54 PM

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
  •