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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 28

Thread: Need help with arrays

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Need help with arrays

    So, I'm working on creating a sort of role playing game. First off, throughout this post I will put little marks to correspond to the code I'm talking about. Down below will be the code organized by the order I reference it in here. To hold all of the enemies I have programmed into the game, I created an array titled enemyList. This holds 100 enemies^1. The first thing that I have my program set to do is call a method titled createAllEnemies()^1 that will first create enemies and then assign them to different places in the array^1. If you're wondering about the creation thing on the enemies, that is the method in the enemy class I created to take variables passed to it and assign it to different variables in the enemy class^2. In any event, when I try to call an enemy for battle^3 it does not in fact give me the enemy I desired but instead gives me the enemy that was last put into the array. I am not getting any errors at all, just the wrong results.

    1: Main class(edited for relevance)

    public class Main  {
       static Enemy[] enemyList = new Enemy[100];
     
       public static void main(String[] args)
       {
          createAllEnemies(); 
       }
     
       private void createAllEnemies() {
            Enemy friend = new Enemy();
            friend.creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10); 
            enemyList[1] = friend;
       }
    }

    2: Enemy class(edited for relevance. Left out an unnecessary method)

    public class Enemy
        {
        static String name;
        static String attackOneName;
        static String attackTwoName;
        static String attackThreeName;
        static int health;
        static int attackOneDamage;
        static int attackTwoDamage;
        static int attackThreeDamage;
        static int experienceGiven;
        static int level;
        static int numberOfAttacks;
        static int maxHealth;
        static int speed;
        public static void creation(String a, String b, String c, String d, int e, int f, int g, int h, int i, int j, int k, int l, int m)
            {
            name = a;
            attackOneName = b;
            attackTwoName = c;
            attackThreeName = d;
            health = e;
            attackOneDamage = f;
            attackTwoDamage = g;
            attackThreeDamage = h;
            experienceGiven = i;
            level = j;
            numberOfAttacks = k;
            maxHealth = l;
            speed = m;
            }   
        }

    3:Example of creating a new battle and passing it an enemy

    Battle battle = new Battle();
            battle.main(Main.enemyList[3]);

    Does anyone have any idea about what could be going wrong here? Any and all help is appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    when I try to call an enemy for battle^3 it does not in fact give me the enemy I desired but instead gives me the enemy that was last put into the array
    Can you post the program's output and add some comments that show what the problem is.


    What does the ^ notation mean?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    Oh sorry the ^3 is to specify that it's the third block of code I posted. And I'm currently using JFrames. My output for that battle thing is that I will if fact not battle the enemy contained in slot 3 of the array, but instead number 99, which was created last. When I don't in fact fill number 99, it defaults to 24(the second to last filled, due to how I'm organizing the array. 99 is sort of a special enemy). If you'd like I can post a screenshot of what should be happening and what is happening, but it would be much easier to just explain. When I run the program, the first battle is with your friend (stored in the first slot of the array). My problem is that when I do actually get to this part, it instead initiates a battle with number 99. So it opens the battle screen perfectly normally, it's just that you aren't facing who you should be. All the stats are of the other enemy, including name and all that. It's as if I'd programmed the game to have you face this other enemy. Sorry if this didn't help. I'm just not sure exactly how to show you the output short of screenshots, and I'm not even sure how effective that'd be.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    To debug this problem you'll probably need to show how all the variables are used and changed so you can catch where in the logic the program is going wrong.
    Try debugging the code by adding lots of printlns that print out the values of the variables as the code executes so you can see what the code is doing.
    To see what is in an array, the Arrays class's toString() method is useful:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));

    A screen print would not show the values of variables which is what you need to see to figure out why the code is doing what it is doing. Print out the variables. If you understand what the program is suppose to do, the printout of the values of the variables should show you where the code is going wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    Oh ok now I get what you're saying. Ok so I put in System.out.println(enemyList[1].name); after every time that something new was put into the array, and it changed to whatever had just been put in, meaning it's overwriting the entire array. Do you know why it's doing this or how I can stop it?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    Print out the index and the contents at that index and the whole array occasionally.
    Be sure to have unique id Strings in the print out so you know which println() has printed what line:
     System.out.println("1 eL[1].name="+enemyList[1].name);

    Do you know why it's doing this or how I can stop it?
    The logic is bad.
    Change the logic.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    So here is the output I created. I just put in a few System.out.println commands in various places. The output shows where I did

    1 eL[1].name=your friend after [1] initialized
    1 eL[1].name=the strange man after [2] initialized
    1 eL[1].name=the goblin after [3] initialized
    2 eL[2].name=the goblin after [3] initialized
    1 eL[1].name=the god after all has initialized
    2 eL[2].name=the god after all has initialized
    3 eL[3].name=the god after all has initialized
    4 eL[4].name=the god after all has initialized
    5 eL[5].name=the god after all has initialized
    6 eL[6].name=the god after all has initialized

    Also, 4-6 aren't even supposed to have something in them at this point. So this tells me that every time I try to fill one spot it fills the whole thing. What could I change to make this not happen? My code basically goes

    Enemy friend = new Enemy();
    friend.creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10);
    enemyList[1] = friend;
     
    Enemy oldMan = new Enemy();
    oldMan.creation("the strange man", "Fireball", "Ice Spear", "Lightning Bolt", 150, 40, 40, 40, 150, 100, 3, 100, 15);
    enemyList[2] = oldMan;
    And so on. What about this might be making it overwrite all other spaces?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    every time I try to fill one spot
    Add some more printlns to show all updates to the array
    also print out the contents of the whole of the array occasionally.

    What about this might be making it overwrite all other spaces?
    A bug in the logic. Add more printlns to show the array's contents after any changes have been made to the array.

    The print outs show that the printlns at 6 locations print the same thing.
    I assume that the leading digit in the print out is the unique id in the String in that println() statement.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    Wait what other printlns do you want me to add. I don't really know what else would be helpful. Also, how would I print out all the contents? Just a println for everything? Because there would be over a few hundred of them.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    how would I print out all the contents
    See post#4

    If the code that is changing elements in the array has a problem, then you need to print out the contents of the array every time a change is made to catch where the problem is.

    Does the array size have to be 100? Can it be reduced for testing so the print outs are smaller?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    Ok thanks. And I guess I could make it smaller, but then I'd have to go back and reassign numbers later. Also, I have the output. So, when I print out all contents of the array, it lists different enemies for each slot(as it should be) and also lists the ones that should be as null, even though when I try to print out the name of something I personally haven't initialized, it still gives the name of whatever was last created. I don't really get it. Here's the output for everything that happens after the first initialization and then after everything. Also, if you're wondering why the first is always null, it's because I never wrote to 0.

    After 1:
    1 eL[1].name=your friend (after [1])
    2 eL[2].name=your friend (after [1])
    3 eL[3].name=your friend (after [1])
    4 eL[4].name=your friend (after [1])
    5 eL[5].name=your friend (after [1])
    6 eL[6].name=your friend (after [1])
    an ID [null, mainFiles.Enemy@239a029e, null, null, null...

    After all:
    1 eL[1].name=the god (after all has initialized)
    2 eL[2].name=the god (after all has initialized)
    3 eL[3].name=the god (after all has initialized)
    4 eL[4].name=the god (after all has initialized)
    5 eL[5].name=the god (after all has initialized)
    6 eL[6].name=the god (after all has initialized)
    an ID [null, mainFiles.Enemy@7a7c3885, mainFiles.Enemy@7ab7b3f9, mainFiles.Enemy@3288df60, null, null, null, ... , null, null, null, mainFiles.Enemy@3801318b, mainFiles.Enemy@565bb966, mainFiles.Enemy@1fe903d5, mainFiles.Enemy@7afaa550, null, null, null, ... ,null, null, null, mainFiles.Enemy@5d7b6643]

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    when I try to print out the name of something I personally haven't initialized, it still gives the name of whatever was last created.
    What is the value of that index? What is the contents of the array at that index as shown by the printout?

    To make the printout of the array more readable add a toString() method to the Enemy class that returns what you want to see printed (the name for example) instead of this: mainFiles.Enemy@7afaa550
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    1 eL[1].name=the god after all has initialized
    2 eL[2].name=the god after all has initialized
    3 eL[3].name=the god after all has initialized
    4 eL[4].name=the god after all has initialized
    5 eL[5].name=the god after all has initialized
    6 eL[6].name=the god after all has initialized
    an ID [null, God, God, God, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, God, God, God, God, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, God]

    So as you can see, it's giving me the name "the god"(what is displayed in game for the last enemy created) for number 6, even though it's null here

    Edit: This is for after all is done.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    You need to print the whole array after ALL updates to it. If you ever get good data into the array, there should be some place in the programs execution where the contents of the array is changed to what you see in the last post. The print out of the other variables will show you where in the execution of the program the big change was made.

    Your print out does not make sense:
    4 eL[4].name=the god after all has initialized
    5 eL[5].name=the god after all has initialized
    6 eL[6].name=the god after all has initialized
    The above shows values for indexes 4, 5 and 6
    The array is null at those indexes:
            0     1     2     3     4     5     6   <<  the indexex
    an ID [null, God, God, God, null, null, null, null, null, null, null, null, null, null, null,
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    No this is in fact after all changes. There aren't very many enemies programmed into the game at the moment. The god is the change to it. If you'd like I can print it step by step through every new enemy

    --- Update ---

    Quote Originally Posted by Norm View Post
    Your print out does not make sense:
    4 eL[4].name=the god after all has initialized
    5 eL[5].name=the god after all has initialized
    6 eL[6].name=the god after all has initialized
    The above shows values for indexes 4, 5 and 6
    The array is null at those indexes:
            0     1     2     3     4     5     6   <<  the indexex
    an ID [null, God, God, God, null, null, null, null, null, null, null, null, null, null, null,
    No I'm aware. That's one of the things confusing me right now

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    If you want to find and fix the problem, you need to print out enough to show you where the problem is.

    Did you see this in my last post? Can you explain how the code printed elements 4-6 when the array at those elements is null?

    Your print out does not make sense:
    4 eL[4].name=the god after all has initialized
    5 eL[5].name=the god after all has initialized
    6 eL[6].name=the god after all has initialized
    The above shows values for indexes 4, 5 and 6
    The array is null at those indexes:
            0     1     2     3    4     5     6   <<  the indexes
    an ID [null, God, God, God, null, null, null, null, null, null, null, null, null, null, null,
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    The code I have written for those numbers(4-6) is perfectly fine.
    System.out.println("4 eL[4].name="+enemyList[4].name + " after all has initialized");
    System.out.println("5 eL[5].name="+enemyList[5].name + " after all has initialized");
    System.out.println("6 eL[6].name="+enemyList[6].name + " after all has initialized");

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    Why does the print out of the whole array show null in elements 4,5,6 ?
    Those slots had values when you printed them individually, but show null in post#13 when the contents of the whole array is printed. Is that expected? Does the code change some of the slots to null?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    No I have no idea why it's doing that,
    Here is all the code I'm using for that part:
    Enemy god = new Enemy();
    god.creation("the god", "Smite", "", "", 9999, 9999, 0, 0, 9999, 9999, 1, 9999, 9999, "God");           
    enemyList[99] = god;
    System.out.println("1 eL[1].name="+enemyList[1].name + " after all has initialized");
    System.out.println("2 eL[2].name="+enemyList[2].name + " after all has initialized");
    System.out.println("3 eL[3].name="+enemyList[3].name + " after all has initialized");
    System.out.println("4 eL[4].name="+enemyList[4].name + " after all has initialized");
    System.out.println("5 eL[5].name="+enemyList[5].name + " after all has initialized");
    System.out.println("6 eL[6].name="+enemyList[6].name + " after all has initialized");
    System.out.println("an ID "+ java.util.Arrays.toString(enemyList));

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    Little bits of code aren't useful to anyone for finding the problem in your code. As I've said many times before: You need to print the whole array after ANY updates to it. After every update to the array, print its full contents.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    1 eL[1].name=your friend (after [1])
    2 eL[2].name=your friend (after [1])
    3 eL[3].name=your friend (after [1])
    4 eL[4].name=your friend (after [1])
    5 eL[5].name=your friend (after [1])
    6 eL[6].name=your friend (after [1])
    an ID [null, Friend, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    1 eL[1].name=the strange man 2nd
    2 eL[2].name=the strange man 2nd
    3 eL[3].name=the strange man 2nd
    4 eL[4].name=the strange man 2nd
    5 eL[5].name=the strange man 2nd
    6 eL[6].name=the strange man 2nd
    an ID [null, Old Man, Old Man, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    1 eL[1].name=the goblin 3rd
    2 eL[2].name=the goblin 3rd
    3 eL[3].name=the goblin 3rd
    4 eL[4].name=the goblin 3rd
    5 eL[5].name=the goblin 3rd
    6 eL[6].name=the goblin 3rd
    an ID [null, Goblin, Goblin, Goblin, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    1 eL[1].name=a bat 4th
    2 eL[2].name=a bat 4th
    3 eL[3].name=a bat 4th
    4 eL[4].name=a bat 4th
    5 eL[5].name=a bat 4th
    6 eL[6].name=a bat 4th
    an ID [null, Bat, Bat, Bat, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, Bat, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    1 eL[1].name=a rat 5th
    2 eL[2].name=a rat 5th
    3 eL[3].name=a rat 5th
    4 eL[4].name=a rat 5th
    5 eL[5].name=a rat 5th
    6 eL[6].name=a rat 5th
    an ID [null, Rat, Rat, Rat, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, Rat, Rat, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    1 eL[1].name=a wolf 6th
    2 eL[2].name=a wolf 6th
    3 eL[3].name=a wolf 6th
    4 eL[4].name=a wolf 6th
    5 eL[5].name=a wolf 6th
    6 eL[6].name=a wolf 6th
    an ID [null, Wolf, Wolf, Wolf, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, Wolf, Wolf, Wolf, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    1 eL[1].name=a boar second to last done
    2 eL[2].name=a boar second to last done
    3 eL[3].name=a boar second to last done
    4 eL[4].name=a boar second to last done
    5 eL[5].name=a boar second to last done
    6 eL[6].name=a boar second to last done
    an ID [null, Boar, Boar, Boar, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, Boar, Boar, Boar, Boar, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null]
    1 eL[1].name=the god after all has initialized
    2 eL[2].name=the god after all has initialized
    3 eL[3].name=the god after all has initialized
    4 eL[4].name=the god after all has initialized
    5 eL[5].name=the god after all has initialized
    6 eL[6].name=the god after all has initialized
    an ID [null, God, God, God, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, God, God, God, God, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, God]

    First 6 spaces in array after each update.
    Code:
    //                 Name  At.1nm  At.2nm  At.3nm  Hp  At.1dm  At.2dm At.3dm Exp. Lv. NmAt. MxHp. Sp.
                Enemy friend = new Enemy();
                friend.creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100, 10, "Friend");
                enemyList[1] = friend;
                System.out.println("1 eL[1].name="+enemyList[1].name + " (after [1])");
                System.out.println("2 eL[2].name="+enemyList[2].name + " (after [1])");
                System.out.println("3 eL[3].name="+enemyList[3].name + " (after [1])");
                System.out.println("4 eL[4].name="+enemyList[4].name + " (after [1])");
                System.out.println("5 eL[5].name="+enemyList[5].name + " (after [1])");
                System.out.println("6 eL[6].name="+enemyList[6].name + " (after [1])");
                System.out.println("an ID "+ java.util.Arrays.toString(enemyList));
     
                Enemy oldMan = new Enemy();
                oldMan.creation("the strange man", "Fireball", "Ice Spear", "Lightning Bolt", 150, 40, 40, 40, 150, 100, 3, 100, 15, "Old Man");
                enemyList[2] = oldMan;
                System.out.println("1 eL[1].name="+enemyList[1].name + " 2nd");
                System.out.println("2 eL[2].name="+enemyList[2].name + " 2nd");
                System.out.println("3 eL[3].name="+enemyList[3].name + " 2nd");
                System.out.println("4 eL[4].name="+enemyList[4].name + " 2nd");
                System.out.println("5 eL[5].name="+enemyList[5].name + " 2nd");
                System.out.println("6 eL[6].name="+enemyList[6].name + " 2nd");
                System.out.println("an ID "+ java.util.Arrays.toString(enemyList));
     
                Enemy goblin = new Enemy();
                goblin.creation("the goblin", "Jab", "Poke", "Club", 150, 20, 30, 40, 100, 5, 3, 150, 10, "Goblin");
                enemyList[3] = goblin;
                System.out.println("1 eL[1].name="+enemyList[1].name + " 3rd");
                System.out.println("2 eL[2].name="+enemyList[2].name + " 3rd");
                System.out.println("3 eL[3].name="+enemyList[3].name + " 3rd");
                System.out.println("4 eL[4].name="+enemyList[4].name + " 3rd");
                System.out.println("5 eL[5].name="+enemyList[5].name + " 3rd");
                System.out.println("6 eL[6].name="+enemyList[6].name + " 3rd");
                System.out.println("an ID "+ java.util.Arrays.toString(enemyList));
     
                Enemy bat = new Enemy();
                bat.creation("a bat", "Screach", "Dive Bomb", "", 400, 10, 20, 0, 30, 2, 2, 30, 20, "Bat");
                enemyList[20] = bat;
                System.out.println("1 eL[1].name="+enemyList[1].name + " 4th");
                System.out.println("2 eL[2].name="+enemyList[2].name + " 4th");
                System.out.println("3 eL[3].name="+enemyList[3].name + " 4th");
                System.out.println("4 eL[4].name="+enemyList[4].name + " 4th");
                System.out.println("5 eL[5].name="+enemyList[5].name + " 4th");
                System.out.println("6 eL[6].name="+enemyList[6].name + " 4th");
                System.out.println("an ID "+ java.util.Arrays.toString(enemyList));
     
                Enemy rat = new Enemy();
                rat.creation("a rat", "Scratch", "Bite", "", 50, 10, 20, 0, 40, 3, 2, 40,25, "Rat");
                enemyList[21] = rat;
                System.out.println("1 eL[1].name="+enemyList[1].name + " 5th");
                System.out.println("2 eL[2].name="+enemyList[2].name + " 5th");
                System.out.println("3 eL[3].name="+enemyList[3].name + " 5th");
                System.out.println("4 eL[4].name="+enemyList[4].name + " 5th");
                System.out.println("5 eL[5].name="+enemyList[5].name + " 5th");
                System.out.println("6 eL[6].name="+enemyList[6].name + " 5th");
                System.out.println("an ID "+ java.util.Arrays.toString(enemyList));
     
                Enemy wolf = new Enemy();
                wolf.creation("a wolf", "Scratch", "Bite", "", 40, 20, 30, 0, 60, 2, 2, 50, 15, "Wolf");
                enemyList[22] = wolf;
                System.out.println("1 eL[1].name="+enemyList[1].name + " 6th");
                System.out.println("2 eL[2].name="+enemyList[2].name + " 6th");
                System.out.println("3 eL[3].name="+enemyList[3].name + " 6th");
                System.out.println("4 eL[4].name="+enemyList[4].name + " 6th");
                System.out.println("5 eL[5].name="+enemyList[5].name + " 6th");
                System.out.println("6 eL[6].name="+enemyList[6].name + " 6th");
                System.out.println("an ID "+ java.util.Arrays.toString(enemyList));
     
                Enemy boar = new Enemy();
                boar.creation("a boar", "Charge", "Gore", "", 50, 20, 30, 0, 60, 2, 2, 50, 10, "Boar");
                enemyList[23] = boar;
                System.out.println("1 eL[1].name="+enemyList[1].name + " second to last done");
                System.out.println("2 eL[2].name="+enemyList[2].name + " second to last done");
                System.out.println("3 eL[3].name="+enemyList[3].name + " second to last done");
                System.out.println("4 eL[4].name="+enemyList[4].name + " second to last done");
                System.out.println("5 eL[5].name="+enemyList[5].name + " second to last done");
                System.out.println("6 eL[6].name="+enemyList[6].name + " second to last done");
                System.out.println("an ID "+ java.util.Arrays.toString(enemyList));
     
     
                Enemy god = new Enemy();
                god.creation("the god", "Smite", "", "", 9999, 9999, 0, 0, 9999, 9999, 1, 9999, 9999, "God");
                enemyList[99] = god;
                System.out.println("1 eL[1].name="+enemyList[1].name + " after all has initialized");
                System.out.println("2 eL[2].name="+enemyList[2].name + " after all has initialized");
                System.out.println("3 eL[3].name="+enemyList[3].name + " after all has initialized");
                System.out.println("4 eL[4].name="+enemyList[4].name + " after all has initialized");
                System.out.println("5 eL[5].name="+enemyList[5].name + " after all has initialized");
                System.out.println("6 eL[6].name="+enemyList[6].name + " after all has initialized");
                System.out.println("an ID "+ java.util.Arrays.toString(enemyList));

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    What do you see when you look at the print out? It looks like there is only one instance of the class being put into all the slots.
    Do you know what it means when a variable is defined as static?
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    I believe so, although I don't see why it would make a difference if the array is defined as being static. Non of the individual values being put into it are static. Is this still making a difference?

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with arrays

    Non of the individual values being put into it are static.
    When did you last look at the code?
    Look at the code in post#1
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Junior Member
    Join Date
    Jan 2013
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Need help with arrays

    No I know that the array itself is static, but why does that make it so that any changes to itself overwrite the whole thing?

Page 1 of 2 12 LastLast

Similar Threads

  1. Help with Arrays
    By xdx in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 12:39 AM
  2. Need help using Arrays!
    By asb23698 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2012, 06:47 PM
  3. Arrays
    By av8 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 25th, 2011, 01:21 PM
  4. 2d arrays help
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2010, 05:27 PM
  5. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 10:23 AM

Tags for this Thread