Results 1 to 16 of 16
Like Tree3Likes
  • 1 Post By sunde887
  • 1 Post By k1ng
  • 1 Post By clint.eastwood

Thread: Declare variables inside a for loop?

  1. #1
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Question Declare variables inside a for loop?

    Hello!

    Im trying to create a cardgame and i want to be able to choose how many players that are going to participate and depending on how many that is i want to create player objects.

    Java Code:
            
    
           
            for (int i=1;i<6;i++) {
            Player player(i) = new Player();
            Cardgame.Deal(ListOfCards, player(i));
            }
    This is just an example of what i am trying to accomplish.
    What i would like this code to do is that it would create 5 new players: player1, player2, player3, player4, player5.

    Is this even possible or do i have to think my code over?

    How would you do if you want to create an x numbers of object dependent on what the player typed in?

    // Clint Eastwood

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Declare variables inside a for loop?

    I suggest you declare an i element array prior to the for loop (or another data structure) and fill it in the loop.

    Java Code:
    for(int i = 0; i < num; ++i){
      players[i] = ...;
    }
    Fubarable likes this.

  3. #3
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    Quote Originally Posted by sunde887 View Post
    I suggest you declare an i element array prior to the for loop (or another data structure) and fill it in the loop.

    Java Code:
    for(int i = 0; i < num; ++i){
      players[i] = ...;
    }
    Ah smart! Thanks! :)

  4. #4
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    Remember that anything declared inside { and } will not exist afer the } so

    Java Code:
    {
      Player p = new Player();
    }
    
    p.getName();
    will be invalid because p no longer exists.

    Java Code:
    ArrayList<Player> players = new ArayList<Player>();
    
    for (int i=1;i<6;i++) {
      Player player = new Player();
      Cardgame.Deal(ListOfCards, player);
    
      players.add(player);
    }
    would allow you access after the loop
    Fubarable likes this.

  5. #5
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    Quote Originally Posted by k1ng View Post
    Remember that anything declared inside { and } will not exist afer the } so

    Java Code:
    {
      Player p = new Player();
    }
    
    p.getName();
    will be invalid because p no longer exists.

    Java Code:
    ArrayList<Player> players = new ArayList<Player>();
    
    for (int i=1;i<6;i++) {
      Player player = new Player();
      Cardgame.Deal(ListOfCards, player);
    
      players.add(player);
    }
    would allow you access after the loop
    Thanks for the help!

    I works but i cant figure out how i will invoke a method for one of the players?

    Like:
    Java Code:
    players.get(0).Method();
    Any Ideas?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,182
    Rep Power
    20

    Default Re: Declare variables inside a for loop?

    Exactly like that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    Yep, simple as that :)

    You see

    Java Code:
    players.get(0);
    returns a reference to the player object at the address 0 in the ArrayList so your code is just like a quick version of doing

    Java Code:
    Player p = players.get(0);
    p.Method()
    so

    Java Code:
    players.get(0).Method();
    works fine.
    Last edited by k1ng; 05-01-2012 at 07:16 PM.

  8. #8
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    Okey because that does not work.

    But Neabeans sugested that i would do it like this:

    Java Code:
            for (int i=0;i<lPlayers.size();i++){
                Player player = (Player) lPlayers.get(i);
                player.AddCard(lDeck);
            }

    After this i can call a method with player.method().

    Is it because i have the class in a different file?

    Can anyone explain why? :)

  9. #9
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    It should work, what is going wrong? What error are you getting?

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Declare variables inside a for loop?

    Follow Java coding conventions. Method names should start with a lowercase letter.
    Code Conventions for the Java Programming Language: Contents

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

  11. #11
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    Quote Originally Posted by k1ng View Post
    It should work, what is going wrong? What error are you getting?
    I get :
    Incompatible types:
    Required: org.me.mylib.Player
    Found: java.lang.object


    If i look in the netbeans hints i get this:
    Cast... get()....to Players

  12. #12
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    Okey, will do this in the future but thats not the problem now right?
    Last edited by clint.eastwood; 05-02-2012 at 07:14 AM.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Declare variables inside a for loop?

    Quote Originally Posted by clint.eastwood View Post
    Okey, will do this in the future but thats not the problem right?
    No, it's not the problem, but it makes your code more difficult to read. The easier your code is on the eyes, the more people will read and understand it, and that translates into quicker and better help for you.

    If i look in the netbeans hints i get this:
    Cast... get()....to Players
    Either use generics, if it's been taught, or cast the returned value. If you haven't been taught either generics or casting, ask your teacher which you are allowed to use and get back to us.

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

  14. #14
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    If you defined the ArrayList the way I did then that shouldn't be a problem i.e.

    Java Code:
    ArrayList<Player> playerList = new ArrayList<Player>();
    because this says that the list can only contain Player types, the thing in pointy braces is 'Generics'. If you defined a standard ArrayList e.g.

    Java Code:
    ArrayList playerList = new ArrayList();
    then this is the same as doing...

    Java Code:
    ArrayList<Object> playerList = new ArrayList<Object>();
    which means that when you take an item off this list you don't know what type it is (because it accepts all objects) so you need to tell it which type to use with 'Casting' such as

    Java Code:
    ArrayList<Object> playerList = new ArrayList<Object>();
    ...
    Player player = (Player) playerList.get(0);
    which says take an <Object> off the list and force it into a <Player> object. This can cause serious issues if you can't be sure it's filled with player objects and it's not very efficient so I recommend typing the ArrayList with generics.
    Last edited by k1ng; 05-02-2012 at 11:21 AM. Reason: spelling

  15. #15
    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Default Re: Declare variables inside a for loop?

    Quote Originally Posted by DarrylBurke View Post
    No, it's not the problem, but it makes your code more difficult to read. The easier your code is on the eyes, the more people will read and understand it, and that translates into quicker and better help for you.


    Either use generics, if it's been taught, or cast the returned value. If you haven't been taught either generics or casting, ask your teacher which you are allowed to use and get back to us.

    db
    I am not in school, I just think it is fun with programming so google and you guys are my teachers :)

    Quote Originally Posted by k1ng View Post
    If you defined the ArrayList the way I did then that shouldn't be a problem i.e.

    Java Code:
    ArrayList<Player> playerList = new ArrayList<Player>();
    because this says that the list can only contain Player types, the thing in pointy braces is 'Generics'. If you defined a standard ArrayList e.g.

    Java Code:
    ArrayList playerList = new ArrayList();
    then this is the same as doing...

    Java Code:
    ArrayList<Object> playerList = new ArrayList<Object>();
    which means that when you take an item off this list you don't know what type it is (because it accepts all objects) so you need to tell it which type to use with 'Casting' such as

    Java Code:
    ArrayList<Object> playerList = new ArrayList<Object>();
    ...
    Player player = (Player) playerList.get(0);
    which says take an <Object> off the list and force it into a <Player> object. This can cause serious issues if you can't be sure it's filled with player objects and it's not very efficient so I recommend typing the ArrayList with generics.
    Thanks k1ing!! Realy good explanation! Works like a charm! :)

    // Clint
    Last edited by clint.eastwood; 05-02-2012 at 07:56 PM. Reason: Spelling
    k1ng likes this.

  16. #16
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

Similar Threads

  1. Replies: 5
    Last Post: 08-20-2014, 11:37 AM
  2. How to prevent sum of variables inside sysout
    By ezee in forum New To Java
    Replies: 1
    Last Post: 10-30-2011, 05:38 AM
  3. Using variables inside methods?
    By Forty0ztoFreedom in forum New To Java
    Replies: 2
    Last Post: 04-08-2011, 08:36 PM
  4. Naming variables inside a cicle
    By jack DANIEL's in forum New To Java
    Replies: 5
    Last Post: 11-01-2010, 08:13 PM
  5. How to declare and initalize application space variables
    By thagentleman in forum New To Java
    Replies: 2
    Last Post: 10-11-2008, 08:19 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
  •