Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Arrow Create a duplicate.

    Hey all,got a simple question here.

    Basically,i'd like to know if there's a way to just call a class,or a section of a class,to make multiple versions of it.

    I.E.

    I want to be able to click somewhere,and just make a square out of one class,then click somewhere else and make a second version of the same class.

    Specifically,i'm making a Bee Game(Don't ask) And I need to be able to make multiple bees on-screen at once. I know how to make one at a time,but i just need to be able to click and make a second/third/ect.

    If there's no just calling one spot,then how?

    Thanks for the help

    ~RiokuTheSlayer

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Create a duplicate.

    Yes. But I believe the problem would be that you can't alter the constructors. One approach would be to make a class with a private constructor so it can't be instantiated. Then have a static method which simply returns an array or list of multiple instances of the class.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    XD I have no idea what you just said(Still relatively new). Could you give me a short example?

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Create a duplicate.

    Java Code:
    class Foo {
       private Foo(<constructor arguments here>) {
          // initialize
       }
       
       public static List<Foo> createInstances(int count, <constructor arguments>) {
            List<Foo> fooList = new ArrayList<>();
            while (count-- > 0) {
                fooList.add(new Foo(<constructor arguments>);
            }
            return fooList;
        }
    }
    
    
    // Usage
    
    List<Foo> myList = Foo.createInstances(10, <constructor arguments>);
    Regards,
    Jim
    Last edited by jim829; 09-17-2013 at 04:19 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    Um,it keeps asking me to turn List<foo> to ArrayList<foo>

    That OK? Or does it need to be List?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Create a duplicate.

    Have you imported List?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Create a duplicate.

    I did make a mistake in the code by not giving count a type (since corrected). However, that was the only problem. It should not complain about return a type of List<Foo>. Make a FooTest class with main in it and try it again. Don't forget to code real constructor args. Keep it small and post your code if you need help.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    Oh,I'm in java 1.6..... I should probably switch to 1.7 XD

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Create a duplicate.

    I keep forgetting to ask them that. My IDE (Eclipse) does it for me.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  10. #10
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    Mine does to,but i heard something about Java 1.7 rendering being much harder,so i didn't want to risk having to re-write all my code. It's my bad,i didn't mention it.

  11. #11
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    Still asking to turn List<> into ArrayList<> ,or remove the <Foo>,Says something about not being Generic.
    Last edited by RiokuTheSlayer; 09-17-2013 at 04:41 AM.

  12. #12
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    Okay,what you told me didn't work in the slightest. Keeps asking me to take away <Foo> From the Array, no matter what I do. Apparently it's something wrong with what you wrote,since it won't even work on a brand new project in the most recent java version.

    Any other ways?

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Create a duplicate.

    What IDE are you using? Even though you may have the latest Java version installed, an earlier version might be used for your project. Right click on the project (usually in the left hand pane) and go through the properties to see what version is actually being used.

  14. #14
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    Java SE 1.7,I'm using Eclipse.

  15. #15
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,931
    Rep Power
    4

    Default Re: Create a duplicate.

    The problem may be the instance construct.

    List<Foo> fooList = new ArrayList<>(); // java 1.7+
    List<Foo> fooList = new ArrayList<Foo>(); // pre-java 1.7

    I checked it out and it works as advertised.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  16. #16
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    Ok,lemmie check again.

  17. #17
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    Create a duplicate.-img.png

    This is what i get,and i also attached a crop of the Project,which shows that i'm using Slick & LWJG,and Java 1.7. Is there a way of using slick?

  18. #18
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    ( I did remove Constructor Arguments since there are none.)

  19. #19
    jbarke12 is offline Member
    Join Date
    May 2013
    Location
    Charleston, South Carolina USA
    Posts
    78
    Rep Power
    0

    Default Re: Create a duplicate.

    Did you have the import for the List?

    Java Code:
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     *
     * @author jbarke12
     */
    public class Testfor {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            List<Bat> bats= new ArrayList<>();
            bats.add(new Bat());
        }
    
        private static class Bat {
    
            public Bat() {
            }
        }
        
        public static List<Bat> getBats(){
        List<Bat> bats= new ArrayList<>();
        return bats;
        }
    }

  20. #20
    Join Date
    Apr 2013
    Location
    Your face.
    Posts
    92
    Rep Power
    0

    Default Re: Create a duplicate.

    -_-

    "import java.awt.List;"

    well,thanks for that,eclipse. Love you to.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 06-03-2012, 07:20 PM
  2. Duplicate Birthday's program
    By tronez in forum New To Java
    Replies: 2
    Last Post: 12-05-2011, 03:29 AM
  3. How do you duplicate a window?
    By Baldie in forum AWT / Swing
    Replies: 6
    Last Post: 06-10-2011, 07:19 AM
  4. Duplicate table
    By anilkumar_vist in forum New To Java
    Replies: 3
    Last Post: 01-09-2010, 11:17 AM
  5. Duplicate XML decleration
    By gyl2009 in forum XML
    Replies: 0
    Last Post: 03-11-2009, 05:13 PM

Posting Permissions

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