如何在类中创建构造函数,其中一个字段是数组(Java)

时间:2022-12-13 21:46:57

So I'm trying to create a class that constructs a football team object but I'm not quite sure how arrays actually work as fields. My constructor looks like this:

我想创建一个类来构造一个足球队的对象,但是我不太确定数组作为字段是如何工作的。我的构造函数是这样的:

public class FootballTeam {
private String[] players;
private String colour;
private int goalDifference;
private Boolean challengeTrophy;

//---------- Construction and Initialisation of the FootballTeam object---------- //
public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
    for (int i = 0; i < 5; i++) {players[i] = aPlayer[i]}
    colour = aColour;
    goalDifference = aGoalDifference;
    challengeTrophy = inChallengeTrophy;
}

I'm really not sure about how to go about writing the constructor so that I can just use an array of strings to test the players part.

我真的不知道如何编写构造函数,这样我就可以使用字符串数组来测试参与者部分。

I also have to create methods that allow for a new player to be added or removed and I'm not sure how I'd structure that, any help would be greatly appreciated; cheers!

我还必须创建方法,允许添加或删除一个新播放器,我不确定我将如何构造,任何帮助都会非常感谢;干杯!

7 个解决方案

#1


3  

You can just:

你可以:

this.players = aPlayer;

If you want to copy array content (as you are doing in for loop), you need to initialize players first.

如果想要复制数组内容(正如在for循环中所做的那样),首先需要初始化播放器。

 this.players = new String[numberOfFields];

#2


1  

I don't really understand what are you trying to do with your String array but if you really want to do that you have to initialise your array like this :

我不太明白你想用你的字符串数组做什么,但是如果你真的想这么做,你必须这样初始化你的数组:

public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
    players = new String[5];
    for (int i = 0; i < 5; i++) {
        players[i] = aPlayer[i];
    }
    colour = aColour;
    goalDifference = aGoalDifference;
    challengeTrophy = inChallengeTrophy;
}

But if you want store your aPlayer in your object you can just :

但是如果你想在你的对象中存储你的aPlayer你可以:

public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
    players = aPlayer;
    colour = aColour;
    goalDifference = aGoalDifference;
    challengeTrophy = inChallengeTrophy;
}

Be careful with this because if you modify one element of players it will also modify aPlayer because you just copy references instead of deep copy of each element of the array.

对此要小心,因为如果您修改一个player元素,它也会修改aPlayer,因为您只是复制引用,而不是对数组的每个元素进行深度复制。

#3


0  

Yes, You did it right. But instead of using a for loop to make both arrays the same you could just put "players = aPlayer;".

是的,你做对了。但是,与其使用for循环使两个数组都相同,不如使用“player = aPlayer;”

#4


0  

Here is the code:

这是代码:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class FootballTeam {
    private Set<String> players;
    private String colour;
    private int goalDifference;
    private Boolean challengeTrophy;

    public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
        players = new HashSet<>(Arrays.asList(aPlayer));
        colour = aColour;
        goalDifference = aGoalDifference;
        challengeTrophy = inChallengeTrophy;
    }

    public void addPlayer(String player) {
        players.add(player);
    }

    public void removePlayer(String player) {
        players.remove(player);
    }
}

#5


0  

You should avoid to do a simple assignation this.players = aPlayer because if your aPlayer content change the players array will be updated too.

你应该避免这样做。玩家= aPlayer因为如果你的aPlayer内容改变了,玩家数组也会被更新。

To do a copy you can do a loop but it may be better to use System.arraycopy or Arrays.copyOf.

要做拷贝,你可以做一个循环,但是最好使用系统。arraycopy或Arrays.copyOf。

#6


0  

Replace the entire for loop part with this line

用这一行替换整个for循环部分

this.players = Arrays.copyOfRange(aPlayer,0,5)

Avoid direct assignment as that's scary because the same reference is repeated in every object. Besides copyofrange also allows you to add first 5 players etc. according to the number you want.

避免直接分配,因为这很可怕,因为相同的引用在每个对象中重复。除了copyofrange之外,你还可以根据你想要的数字添加前5名球员等等。

#7


0  

Try following code with you constructor.

尝试使用构造函数执行以下代码。

Sample Code

示例代码

public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
    players = new String[aPlayer.length];
    players = aPlayer;
    colour = aColour;
    goalDifference = aGoalDifference;
    challengeTrophy = inChallengeTrophy;
}

Here first you need to initialize size of array than you can initialize entire array value.

这里,首先需要初始化数组的大小,而不是初始化整个数组的值。

Hope it will help you easily.

希望它能帮助你。

#1


3  

You can just:

你可以:

this.players = aPlayer;

If you want to copy array content (as you are doing in for loop), you need to initialize players first.

如果想要复制数组内容(正如在for循环中所做的那样),首先需要初始化播放器。

 this.players = new String[numberOfFields];

#2


1  

I don't really understand what are you trying to do with your String array but if you really want to do that you have to initialise your array like this :

我不太明白你想用你的字符串数组做什么,但是如果你真的想这么做,你必须这样初始化你的数组:

public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
    players = new String[5];
    for (int i = 0; i < 5; i++) {
        players[i] = aPlayer[i];
    }
    colour = aColour;
    goalDifference = aGoalDifference;
    challengeTrophy = inChallengeTrophy;
}

But if you want store your aPlayer in your object you can just :

但是如果你想在你的对象中存储你的aPlayer你可以:

public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
    players = aPlayer;
    colour = aColour;
    goalDifference = aGoalDifference;
    challengeTrophy = inChallengeTrophy;
}

Be careful with this because if you modify one element of players it will also modify aPlayer because you just copy references instead of deep copy of each element of the array.

对此要小心,因为如果您修改一个player元素,它也会修改aPlayer,因为您只是复制引用,而不是对数组的每个元素进行深度复制。

#3


0  

Yes, You did it right. But instead of using a for loop to make both arrays the same you could just put "players = aPlayer;".

是的,你做对了。但是,与其使用for循环使两个数组都相同,不如使用“player = aPlayer;”

#4


0  

Here is the code:

这是代码:

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class FootballTeam {
    private Set<String> players;
    private String colour;
    private int goalDifference;
    private Boolean challengeTrophy;

    public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
        players = new HashSet<>(Arrays.asList(aPlayer));
        colour = aColour;
        goalDifference = aGoalDifference;
        challengeTrophy = inChallengeTrophy;
    }

    public void addPlayer(String player) {
        players.add(player);
    }

    public void removePlayer(String player) {
        players.remove(player);
    }
}

#5


0  

You should avoid to do a simple assignation this.players = aPlayer because if your aPlayer content change the players array will be updated too.

你应该避免这样做。玩家= aPlayer因为如果你的aPlayer内容改变了,玩家数组也会被更新。

To do a copy you can do a loop but it may be better to use System.arraycopy or Arrays.copyOf.

要做拷贝,你可以做一个循环,但是最好使用系统。arraycopy或Arrays.copyOf。

#6


0  

Replace the entire for loop part with this line

用这一行替换整个for循环部分

this.players = Arrays.copyOfRange(aPlayer,0,5)

Avoid direct assignment as that's scary because the same reference is repeated in every object. Besides copyofrange also allows you to add first 5 players etc. according to the number you want.

避免直接分配,因为这很可怕,因为相同的引用在每个对象中重复。除了copyofrange之外,你还可以根据你想要的数字添加前5名球员等等。

#7


0  

Try following code with you constructor.

尝试使用构造函数执行以下代码。

Sample Code

示例代码

public FootballTeam(String[] aPlayer, String aColour, int aGoalDifference, Boolean inChallengeTrophy) {
    players = new String[aPlayer.length];
    players = aPlayer;
    colour = aColour;
    goalDifference = aGoalDifference;
    challengeTrophy = inChallengeTrophy;
}

Here first you need to initialize size of array than you can initialize entire array value.

这里,首先需要初始化数组的大小,而不是初始化整个数组的值。

Hope it will help you easily.

希望它能帮助你。