1

I am trying to create a class that creates arrays for a project of mean I have been killing my self on how to do it

public class Team {

    private Object arr;

    void createTeam(){
        String[] arr = new String[15];
    }
}

0xh3xa
  • 4,801
  • 2
  • 14
  • 28
Lynn Long
  • 11
  • 1
  • While you are at it you can initialize the array with a default value `Arrays.fill(arr, "deault value");` – c0der May 20 '20 at 03:39

3 Answers3

1

Just after creating the array using new assign it to the arr reference if you want to use arr in Team class

    public class Team {

        private String[] arr;

        void createTeam() {
            arr = new String[15];
        }
    }

, if you want to call createTeam from another class and use like

    public class Team {

        public static String[] createTeam() {
            return new String[15];
        }
    }

Usage from another class

String[] arr = Team.createTeam();
0xh3xa
  • 4,801
  • 2
  • 14
  • 28
0

Let me straighten out a bit of terminology - the class contains one or more methods; I think you want a method to create an array when called.

It would be most useful if, after the array is created, it was returned to the caller; since you talk of doing this more than once, it makes sense for the caller to do it as many times as it wants:

public class Team {

    pubilc String[] createTeam() {
        String[] arr = new String[15];
        return arr;
    }
}

Now some other piece of code can do this:

...
Team team = new Team();
String[] teamArray = team.createTeam();

and teamArray has been created and returned to the caller for whatever it wants it for. Note that, in this case, no copy of the team variable exists in the Team object once the createTeam() method has returned.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • As a factory method you may want to make it static – c0der May 20 '20 at 03:45
  • I absolutely would; I didn't mention it in this answer since the OP appears to be new at this and the concept of 'static' is one more thing for them to understand. – arcy May 20 '20 at 10:19
0

Actually, if you want to access your newly created array from another class you simply can't, because your String[] is a private attribute and your package scoped method does return a void.

If you only need to create a String[] for other classes, then you don't need the attribute "arr", you could also overload your method to create an array of desired size:

public class Team {

public static String[] createTeam(){ return new String[15]; }

public static String[] createTeam(int n){ return new String[n]; }

}

If you need your String[] for a further manipulation in your "Team" class, then you should keep it private and use a getter to access it from other classes :

public class Team {

private String[] arr;

public void createTeam(){ this.arr = new String[15]; }

public String[] getArr() { return arr; }

}

if you're trying to cast a String[] into your arr attribute, your method doesn't work because Java considers String[] arr in the method as a local variable and not as class attribute, then you should remove "String[]" before arr.

maklil
  • 1
  • 2