Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: dynamically resizing 2d array

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default dynamically resizing 2d array

    i am in need of an efficient way to resize a 2d array. the 2d array must be able to increase and decrease in size very frequently.

    currently i am doing it the least efficient way possible.
    public void ArrayResize(int rows , int columns){
    	    	holdArray=new int[iMapTiles.length][iMapTiles[0].length];//copy the current array into temporary array
    	    	for (int r=0; r < holdArray.length; r++){
    	    		for (int c=0; c < holdArray[0].length; c++) 
    					holdArray[r][c]=iMapTiles[r][c];
    			} 	
     
     
    	    	iMapTiles= new int[rows][cols];// reinstantiate the array to be the wished size
     
    	        if(rows>holdArray.length||columns>holdArray[0].length){//copy the temporary array into the new array
    		    	for (int j=0; j < holdArray.length; j++)
    		    		for (int k=0; k < holdArray[0].length; k++) 
    		    			iMapTiles[j][k]=holdArray[j][k];
    	    	}else{
    	    		for (int j=0; j <rows; j++)
    		    		for (int k=0; k <columns; k++) 
    		    			iMapTiles[j][k]=holdArray[j][k];
    	    	}
     
        }


    this works, but it is very slow since i re-instantiate the arrays very often. i was thinking of either implementing a 2d arraylist(seems hard) or just using variables to hold the size of the array and instantiate the array to be bigger than it actually is, then i would only have to resize it every once in a while(like an arraylist).

    any tips? thanks
    Last edited by Flamespewer; March 1st, 2010 at 05:40 PM.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: dynamically resizing 2d array

    I'm wondering why you would want to do this, as you are saying you should just use an ArrayList

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: dynamically resizing 2d array

    You can take a look at this.

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. [SOLVED] How to dynamically create ArrayLists (or something similar)?
    By igniteflow in forum Collections and Generics
    Replies: 4
    Last Post: August 6th, 2009, 06:00 AM
  4. Add Jmol applet dynimically in JSF(java server faces)
    By megha in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: May 15th, 2009, 06:16 AM
  5. JAVA Image Icon and JButton resizing problem
    By antitru5t in forum AWT / Java Swing
    Replies: 1
    Last Post: March 13th, 2009, 04:39 AM