Java / OO新手问题:
main实例化Track类。 现在我希望该对象 - track1 - 实例化许多Clip对象,其数量直到运行时才会被知道。 从Track类里面我可以像这样创建一个新的Clip :
Clip clip1 = new Clip(); // this is track1.clip1 from main但是当我想根据用户输入添加更多剪辑时,我该如何以及在何处进行此操作? 从main ,像:
Clip track1.clipX = new Clip();当我不知道会有多少个时,如何命名和引用Clip对象?
Java/OO newbie question:
main instantiates Track class. Now I want that object--track1--to instantiate many Clip objects, the number of which will not be known until runtime. From inside the Track class I can make a new Clip like this:
Clip clip1 = new Clip(); // this is track1.clip1 from mainBut when I want to add more clips based on user input, how and where do I do this? From main, something like:
Clip track1.clipX = new Clip();How do I name and reference Clip objects when I don't know how many there will be?
最满意答案
您将使用Collection(如List或Set)来保存多个Clip对象:
int numberOfClips = 10; List<Clip> clips = new ArrayList<Clip>(); for (int i = 0; i < numberOfClips; i++) { Clip clip = new Clip(); clips.add(clip); }这会将10个Clip对象添加到数组clips 。 您可以根据用户的输入设置numberOfClips,而不是硬编码为10。
List Java docs页面描述了您可以在Lists上调用的所有方法,以便向您展示如何获取List中的项目,从List中删除项目等。
You'd use a Collection (like a List or Set) to hold the multiple Clip objects:
int numberOfClips = 10; List<Clip> clips = new ArrayList<Clip>(); for (int i = 0; i < numberOfClips; i++) { Clip clip = new Clip(); clips.add(clip); }This would add 10 Clip objects to the array clips. Instead of hardcoding to 10, you could set numberOfClips based on the user's input.
The List Java docs page describes all of the methods you can call on Lists, so that will show you how to get items in the List, remove items from the List, etc.
(Java新手)实例化未知数量的对象((Java newbie) Instantiating unknown number of objects)Java / OO新手问题:
main实例化Track类。 现在我希望该对象 - track1 - 实例化许多Clip对象,其数量直到运行时才会被知道。 从Track类里面我可以像这样创建一个新的Clip :
Clip clip1 = new Clip(); // this is track1.clip1 from main但是当我想根据用户输入添加更多剪辑时,我该如何以及在何处进行此操作? 从main ,像:
Clip track1.clipX = new Clip();当我不知道会有多少个时,如何命名和引用Clip对象?
Java/OO newbie question:
main instantiates Track class. Now I want that object--track1--to instantiate many Clip objects, the number of which will not be known until runtime. From inside the Track class I can make a new Clip like this:
Clip clip1 = new Clip(); // this is track1.clip1 from mainBut when I want to add more clips based on user input, how and where do I do this? From main, something like:
Clip track1.clipX = new Clip();How do I name and reference Clip objects when I don't know how many there will be?
最满意答案
您将使用Collection(如List或Set)来保存多个Clip对象:
int numberOfClips = 10; List<Clip> clips = new ArrayList<Clip>(); for (int i = 0; i < numberOfClips; i++) { Clip clip = new Clip(); clips.add(clip); }这会将10个Clip对象添加到数组clips 。 您可以根据用户的输入设置numberOfClips,而不是硬编码为10。
List Java docs页面描述了您可以在Lists上调用的所有方法,以便向您展示如何获取List中的项目,从List中删除项目等。
You'd use a Collection (like a List or Set) to hold the multiple Clip objects:
int numberOfClips = 10; List<Clip> clips = new ArrayList<Clip>(); for (int i = 0; i < numberOfClips; i++) { Clip clip = new Clip(); clips.add(clip); }This would add 10 Clip objects to the array clips. Instead of hardcoding to 10, you could set numberOfClips based on the user's input.
The List Java docs page describes all of the methods you can call on Lists, so that will show you how to get items in the List, remove items from the List, etc.
发布评论