Results 1 to 20 of 27
Thread: Create a duplicate.
- 09-17-2013, 03:56 AM #1
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
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
- 09-17-2013, 04:48 AM #2
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-17-2013, 04:51 AM #3
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
Re: Create a duplicate.
XD I have no idea what you just said(Still relatively new). Could you give me a short example?
- 09-17-2013, 04:56 AM #4
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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>);
JimLast edited by jim829; 09-17-2013 at 05:19 AM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-17-2013, 05:15 AM #5
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
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?
- 09-17-2013, 05:17 AM #6
Re: Create a duplicate.
Have you imported List?
- 09-17-2013, 05:24 AM #7
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-17-2013, 05:24 AM #8
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
Re: Create a duplicate.
Oh,I'm in java 1.6..... I should probably switch to 1.7 XD
- 09-17-2013, 05:26 AM #9
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Create a duplicate.
I keep forgetting to ask them that. My IDE (Eclipse) does it for me.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-17-2013, 05:27 AM #10
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
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.
- 09-17-2013, 05:38 AM #11
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
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 05:41 AM.
- 09-17-2013, 07:52 AM #12
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
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?
- 09-17-2013, 07:58 AM #13
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.
- 09-17-2013, 08:01 AM #14
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
Re: Create a duplicate.
Java SE 1.7,I'm using Eclipse.
- 09-17-2013, 01:42 PM #15
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-17-2013, 08:30 PM #16
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
Re: Create a duplicate.
Ok,lemmie check again.
- 09-17-2013, 08:51 PM #17
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
- 09-17-2013, 08:52 PM #18
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
Re: Create a duplicate.
( I did remove Constructor Arguments since there are none.)
- 09-17-2013, 08:57 PM #19
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; } }
- 09-17-2013, 09:02 PM #20
Member
- Join Date
- Apr 2013
- Location
- Your face.
- Posts
- 94
- Rep Power
- 0
Similar Threads
-
Compile Error: Duplicate Class.. (after package) but I don't have a duplicate class..
By JimmyD in forum Advanced JavaReplies: 5Last Post: 06-03-2012, 08:20 PM -
Duplicate Birthday's program
By tronez in forum New To JavaReplies: 2Last Post: 12-05-2011, 04:29 AM -
How do you duplicate a window?
By Baldie in forum AWT / SwingReplies: 6Last Post: 06-10-2011, 08:19 AM -
Duplicate table
By anilkumar_vist in forum New To JavaReplies: 3Last Post: 01-09-2010, 12:17 PM -
Duplicate XML decleration
By gyl2009 in forum XMLReplies: 0Last Post: 03-11-2009, 06:13 PM
Bookmarks