Code: public Channel[] getChannels(){ Iterator<Channel> list = channels.iterator(); Channel[] array = {}; while(list.hasNext()){ array[array.length] = list.next(); } return array; } A Channel is one of my custom classes. My question is: can I do it like this? Will it work? If not, how do I get an array of channels?
I don't think you can add new entries to an array, so either define the array with the size of channels or use an ArrayList and it's toArray() function. There may be other/simpler solutions but I don't know how your channels class looks like :/ Arrgh, sry... -.- EDIT by Moderator: merged posts, please use the edit button instead of double posting.
If you have a .toArray(), use it. Otherwise, implement it: * If you have a way of getting the number of channels, use that. * If you have no idea how many channels there are, you can count them with an iterator. * Once you know how many there are, Channel[] channels = new Channel[count]; allocates an array. * Then you need to walk the channels again, copying each one into each slot of the channels array, using an iterator and an index. * Return that array. I find it dangerous to call your iterator variable list. A list is a kind of collection, an iterator is like a finger pointing at a specific place within a collection. It becomes easier to get the concepts confused if you mix and match.
The iterator is the object by which you can traverse through a Java collection of unknown size. The for loop is an example of Java syntax by which you can use an iterator. Code: int count = 0; Iterator<Type> it = collection.getIterator(); while (it.hasNext()) { Type element = it.next(); count++; } is equivalent to Code: int count = 0; for (Iterator<Type> it = collection.getIterator(); it.hasNext(); it.next()) { count++; } is equivalent to (the newest syntax added to Java) Code: int count = 0; for (Type element : collection) { count++; }
@Lolmewn - just do Channel[] channels = list.toArray(new Channel[0]); I'm assuming you're using standard java collections for it since you named it list, and are using an iterator on it.
Sort of. I'm now doing it like this: Code: public List<Player> getPlayers(){ List<Player> players = new ArrayList<Player>(); Iterator<String> it = pList.iterator(); while(it.hasNext()){ Player p = Bukkit.getServer().getPlayer(it.next()); if(p == null){ continue; }else{ players.add(p); } } return players; }
woops misread! - soo what's the issue with doing what your're doing? - imo you should keep a list of players currently in the channel rather than iterating over the channel list everytime (And use PlayerLeave to remove them automatically). On top of that I'd see multiple reasons why you'd want to use a Set rather than a list.
@Lolmewn - think of a Set as in mathematics. A Set can contain multiple instances of a specific base type. However, there can only ever be one unique instance of a variable in the set. The usual purpose of a set is to determine whether something exists within it, such as, in your case, which users are on a particular Channel - Sets have a fast contains() and remove() method when compared to Lists. The drawbacks are that iteration is usually slightly slower (only a little) than in a List, and there aren't any getter methods (not so much a drawback as part of what the Set represents). Sets also have useful combining methods, such as complement(), union(), etc. Code: Set<Player> players = new HashSet<Player>(); players.add(myPlayer); boolean in = players.contains(myPlayer); // fast
Hmm.. It seems I cannot convert the toArray() to a Participants[], although I'm absolutely sure there are only Participants in it!