Closed Thread
Results 1 to 4 of 4

Thread: Really Quick Question about Array Initialization

  1. #1
    Panzercrisis is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Really Quick Question about Array Initialization

    I've only known a very small amount of Java in the past, and now I'm even having to refresh my memory on the most extreme basics. My background is mainly in C++.

    Let's say you have an int array and want to initialize that array upon declaration to some starting values or whatever.

    Code:
    int arry[] = {1, 2, 3};
    The basic way to do that is easy to locate on the Internet. However let's say you're trying to do that with a class that only employs a constructor that takes multiple arguments.

    Code:
    public class position extends object {
         /* an almost pointless class */
         public position(int x, int y) {
              xPos = x;
              yPos = y;
         }
    
         public int xPos;
         public int yPos;
    }
    Yes, I know that technically the array can be filled in sometime after declaration, but what if I don't want to do that? What if I want to initialize the array instantly like with the int array above? How do I handle the fact that the class's only constructor (if there is a default constructor put there anyway, I don't want to use it) has multiple parameters? That is something that is not so easy to find on the Internet. Thanks!

    Code:
    position map[] = ????

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,881
    Rep Power
    24

    Re: Really Quick Question about Array Initialization

    so do you mean something like:
    Code:
    public class Test {
        int[] intArray;
    
        public Test(int a, int b, int c) {
            int[] tempIntArray = {a,b,c};
            intArray = tempIntArray;
        }
    }
    OR (probably better)
    Code:
    public class Test {
        int[] intArray;
    
        public Test(int a, int b, int c) {
            intArray = new int[] {a,b,c};
        }
    }
    ?

  4. #3
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: Really Quick Question about Array Initialization

    According to java convention, all Class names are starting with Capital Letters.

    Code:
    public class Test{
    	Position pos[] = { 
    			new Position(1, 2), 
    			new Position(3, 4), 
    			new Position(5, 6) 
    	};
    }
    
    
    class Position{
        /* an almost pointless class */
        public Position(int x, int y) {
             xPos = x;
             yPos = y;
        }
    
        public int xPos;
        public int yPos;
    }
    I'm exactly not sure what you meant. If you don't want to fill it instantly, then write:
    Code:
    Position pos[] = new Position[yourArrayLength]();

  5. #4
    Panzercrisis is offline Newbie
    Join Date
    Feb 2010
    Posts
    2
    Rep Power
    0

    Re: Really Quick Question about Array Initialization

    Yes, that worked out quite well. Thank you. The example you showed that had the three consecutive new Position declarations in class Test was basically what I asking for. It's quite easy to find the information on how to initialize an array upon declaration when you're just dealing with stuff like integers for instance, but obviously you can't just say (in those examples)
    Code:
    Position arry[] = { new Position, new Position };
    nor can you say
    Code:
    Position arry[] = {{2, 3}, {5,6}};
    So I was just wondering about the syntax, which you answered. Thanks!

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. quick question about c++
    By even821 in forum C and C++
    Replies: 4
    Last Post: 10-05-2011, 11:52 AM
  2. Quick question with CSS
    By system32 in forum Website Design
    Replies: 3
    Last Post: 07-11-2010, 09:39 AM
  3. Initialization List Array Members
    By Buttacup in forum C and C++
    Replies: 8
    Last Post: 01-03-2010, 08:58 PM
  4. quick question
    By Andrew.G in forum General Programming
    Replies: 6
    Last Post: 09-18-2009, 12:29 AM
  5. quick question (C)
    By johnobri in forum C and C++
    Replies: 1
    Last Post: 04-30-2009, 09:14 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts