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: creating an array before knowing the amount of values

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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[][] boardint lengthint 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 board.length i++) // i run over rows
        
    for(int j board[0].length j++) // j run over columns
           
    if(placeValid(boardijwidthlength)) { // 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 coards.length-;  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 0ij++){
          
    ans[j][0] = coards[j][0];
            
    ans[j][1] = coards[j][1];
         }
            return 
    ans;
         }
          return 
    coards;
        } 


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: creating an array before knowing the amount of values

    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.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: creating an array before knowing the amount of values

    Quote Originally Posted by pbrockway2 View Post
    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.

Similar Threads

  1. Creating an array in constructor ... defaults all values?
    By mwebb in forum Object Oriented Programming
    Replies: 2
    Last Post: February 19th, 2012, 03:06 PM
  2. Testing values in an array.
    By bholzer in forum Loops & Control Statements
    Replies: 7
    Last Post: September 28th, 2011, 05:18 PM
  3. Problem with array values
    By Harry Blargle in forum Collections and Generics
    Replies: 5
    Last Post: September 17th, 2011, 04:05 PM
  4. How do I draw an image, knowing the co-ordinates and rgb values??
    By byrtis in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 25th, 2011, 09:15 AM
  5. need help inputting values into an array
    By pds8475 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 08:47 PM