Can I do this..?

Discussion in 'Plugin Development' started by Lolmewn, Nov 23, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Lolmewn Retired Staff

    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?
     
  2. Offline

    V10lator

    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.
     
    Last edited by a moderator: May 21, 2016
  3. Offline

    halley

    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.
     
  4. Offline

    Lolmewn Retired Staff

    Oh ye, forgot there was an toArray function :p
    Nice, will use that.
     
  5. Offline

    silthus

    Is counting them with an iterator the same as counting them with an for each loop?
     
  6. Offline

    halley

    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++; }
    
     
  7. Offline

    Sleaker

    @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.
     
  8. Offline

    Lolmewn Retired Staff

    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;
    	}
    
     
  9. Offline

    Sleaker

    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.
     
  10. Offline

    Lolmewn Retired Staff

    Never used Set, got any examples?
     
  11. Offline

    fullwall

    @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
    
     
  12. Offline

    ZNickq

    1.Use a HashSet! (like @fullwall said)
    2.PLEASE, no more chat plugins! :/
     
  13. Offline

    Lolmewn Retired Staff

    1. Oh, HashSet, K sure!
    2. For my server only ;)
     
    ZNickq likes this.
  14. Offline

    Lolmewn Retired Staff

    Hmm.. It seems I cannot convert the toArray() to a Participants[], although I'm absolutely sure there are only Participants in it!
     
  15. Offline

    ZNickq

    are you using a HashSet?
    and why are you trying to convert it to an array? >.>
     
  16. Offline

    Lolmewn Retired Staff

    I want to get all values from the HashSet.
     
  17. Offline

    ZNickq

    for(Value val: hashset)
    {
    //do stuff with val
    }
     
  18. Offline

    Lolmewn Retired Staff

    oh lol I fail.
    Thanks.
     
Thread Status:
Not open for further replies.

Share This Page