creating an array before knowing the amount of values

• December 12th, 2012, 02:08 AM
tirasd
creating an array before knowing the amount of values
first of all, sorry about my bad english and thx for the time you spend reading this.

this is a functios from a problem of 2D packing( a 2d boards with 2d tiles, like the knapsack, returning boolean if the tiles can fit iniside the board)

i tried to make a function that getting a board in different situations, and a tile, and return an array wich contain all the coardinates that avaliable for the corrent tile.
for example, a board as big as 2X2, and a tile of 1x1, the function will return an array like:

00
01
10
11

the problem is with the fact that i dont know how many coards will be, before initalizing the array.
so i managed to make another function that removing the unwanted "000000" in the end.
(sound aufull, i know, but i cant use Objects because we didnt leaned it yet)

the problem starts when my function sometimes leaves another 0,0 in the end.

any alternative or solutions??

PHP Code:

``` public static int[][] allAvaliablePlaces(int[][] board, int length, int width){     int[][] coards = new int[board.length*board[0].length][2];      int index = -1; // will index the new array, and chek if there are coardinates at all     for(int i = 0 ; i < board.length ; i++) // i run over rows     for(int j = 0 ; j < board[0].length ; j++) // j run over columns        if(placeValid(board, i, j, width, length)) { // chek if the place is valid         index++;         coards[index][0] = i; // the row component         coards[index][1] = j; // the col comp                       }         int[][] finalcoards = cuttingZeroes(coards);                        if(index == -1)       finalcoards = null;  // returns null if index is same as the begining     return finalcoards;     }     public static int[][] cuttingZeroes(int[][] coards) {     for( int i = 0 ; i < coards.length-1 ;  i++)      if(coards[i][0] == coards[i][1] && coards[i+1][0] == coards[i+1][1] && coards[i][0] == 0)     {        int[][] ans = new int[i][2];      for(int j = 0; j < i; j++){       ans[j][0] = coards[j][0];         ans[j][1] = coards[j][1];      }         return ans;      }       return coards;     }  ```
• December 12th, 2012, 02:20 AM
pbrockway2
Re: creating an array before knowing the amount of values
Quote:

the problem starts when my function sometimes leaves another 0,0 in the end
Could you use some other value as a "placeholder" - like -1 which won't be confused with real data?

Really though you need something better than arrays. Arrays have an unchanging length that is set when they are created. Lists grow as needed making them more useful. If it's homework you have no choice, but if not then your efforts would be better spent on learning about lists.
• December 12th, 2012, 02:24 AM
tirasd
Re: creating an array before knowing the amount of values
Quote:

Originally Posted by pbrockway2
Could you use some other value as a "placeholder" - like -1 which won't be confused with real data?

Really though you need something better than arrays. Arrays have an unchanging length that is set when they are created. Lists grow as needed making them more useful. If it's homework you have no choice, but if not then your efforts would be better spent on learning about lists.

those are homework :\
and i cant use somthing we didnt learn about in class.