Solved Array Problems

Discussion in 'Plugin Development' started by PreFiXAUT, Mar 2, 2014.

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

    PreFiXAUT

    Hey Guys, I'm having troubles with using Arrays.
    Basicly, I need to add Data into the Array, but I don't know how many data there is, so I can't set it to a fix size.
    Currently it's looking like this:
    Show Spoiler

    Code:java
    1. public static String[] getGroups(String playername) {
    2. checkFile();
    3. try {
    4. reader = new FileReader(file);
    5. BufferedReader br = new BufferedReader(reader);
    6. String read;
    7. String[] output = null;
    8. while ((read = br.readLine()) != null) {
    9. String[] data = read.split(",");
    10. for (int i=0; i < data.length; i++) {
    11. if (data[i].equalsIgnoreCase(playername)) {
    12. output[output.length] = data[0];
    13. }
    14. }
    15. }
    16. br.close();
    17. reader.close();
    18. return output;
    19. } catch (Exception e) {
    20. e.printStackTrace();
    21. return null;
    22. }
    23. }[/i]


    Can someone help me? I'm realy having troubles with this simple thing, but I've never needed it before, so hopefully someone can help me.

    Thanks in advance.
     
  2. Offline

    alex123099

    PreFiXAUT
    For these kinds of tasks, Java has dynamically expanding lists. You could use a simple ArrayList<Object> to do what you need, and then it also has a toArray method to turn the list to an array if you need to.
     
  3. Offline

    hexaan

    PreFiXAUT
    I would just use an ArrayList.

    Code:java
    1.  
    2. ArrayList<String> output = new ArrayList();
    3. output.add("TestString");
    4. output.add("MoreStrings");
    5. //Getting the size
    6. output.size();
    7. //Getting data
    8. output.get(0);
    9.  
     
  4. Offline

    PreFiXAUT

Thread Status:
Not open for further replies.

Share This Page