有没有一种方法将数组对象添加到一个类中?

huangapple go评论119阅读模式
英文:

Is there a way to add array objects to a class?

问题

我对这个非常新,但我正在尝试在Android Studio中向类中添加对象数组。我已经创建了一个名为“User”的类,将用于存储每个用户的信息。我需要为每个用户创建一个对象,并设置适当的变量(名称、密码和喜好颜色),然后将其存储在数据结构中。如下面的代码所示,我一直在努力找出最佳方法,但我仍然不确定。如果有人能帮忙或给我任何指导,我将非常感谢。

public class User {

String user1[] = new String[3];
String user2[] = new String[3];
String user3[] = new String[3];
String user4[] = new String[3];
String user5[] = new String[3];

String user[] = {"Jason", "Sword", "Red"};

}
英文:

I am very new to this but I am trying to add object arrays to a class in android studio. I have created a "User" class that will be used to store information of each user. I need to create an object for each user, and set the appropriate variables (name, password and favourite color), and store this in a data structure. As you can see in the code below ive been trying to figure out the best way to go about this but I am still unsure. If anyone can please help or give me any guidance it would be muchly appreciated.

public class User {

String user1[] = new String[3];
String user2[] = new String[3];
String user3[] = new String[3];
String user4[] = new String[3];
String user5[] = new String[3];

String user[] = {"Jason", "Sword", "Red"};

}

答案1

得分: 1

我认为你需要稍微调整你的结构。

你上面提供的类实际上不是一个用户(User),而是一组用户(Users)。我建议你相应地重新命名该类,例如 UserGroup。'Users' 也完全可以,但如果有人在代码中快速浏览,他们可能会错误地认为自己看到了 'User',然后感到困惑。我相当确定我会这样!

现在创建一个不同的名为 User 的类,其中包含一些用于存储不同值的字符串字段,例如:

public class User {
  String name;
  String weapon;

  public User(String name, String weapon) {
    this.name = name;
    this.weapon = weapon;
  }
}

在你的 UserGroup 类中,不要持有字符串数组,而是实际的用户对象,例如:

public class UserGroup {

  User user1 = new User("Jason", "Sword");

}
英文:

I think you need to change your structure a bit.

The class you have given us above isn't really a User, but a group of Users. I suggest you rename the class accordingly, e.g. UserGroup. 'Users' would also work just fine, but if someone is skimming quickly through the code they might think they read 'User' instead and get muddled. I am pretty sure I would!

Go and make a different class called User, which has a bunch of String fields in it for the different values you want to store, e.g.

public class User {
  String name;
  String weapon;

  public User(String name, String weapon) {
    this.name = name;
    this.weapon = weapon;
  }

}

Back in your UserGroup class, don't hold String arrays but actual Users, e.g.

public class UserGroup {

  User user1 = new User("Jason", "Sword");

}

答案2

得分: 0

因为您正在使用Android Studio,我猜您想要制作一个应用程序。所以,如果这是您的想法,您必须将用户数据存储在文件中,因为类是一个逻辑结构。类仅在运行时起作用。所以当您使用类时,在运行时之后它会销毁用户数据,这实际上没有任何用处。最好使用文件和对象,并且不要忘记为密码使用安全哈希算法。

英文:

As you are using Android Studio, I suppose that you wanna make an app. So if that's the idea you must store user data in files because class is a logical construct. class works only at runtime. So when you use a class, after runtime it will destroy the user data which is practically of no use. It's better to use files and Objects and don't forget to use Secure Hash Algorithms for password.

答案3

得分: 0

你可以使用两个哈希映射。
'UserAPass' 用于存储用户名和密码。
'UserAFavCol' 用于存储用户名和喜欢的颜色。
将这两个哈希映射设置为静态,以便根据应用程序的使用在实例之间共享。

import java.util.HashMap;

public class User {
    static HashMap<String, String> UserAPass = new HashMap<String, String>();
    static HashMap<String, String> UserAFavCol = new HashMap<String, String>();
    
    void addUser(String name, String pass, String col) {
        UserAPass.put(name, pass);
        UserAFavCol.put(name, col);
    }
}
英文:

You can use two Hash Map.
'UserAPass' is used to store name and password.
'UserAFavCol' is used to store name and favorite color.
Put both hash maps as static so that it can be shared among instances as per app usage.

import java.util.HashMap;
public class User {
static HashMap&lt;String, String&gt; UserAPass = new HashMap&lt;String, String&gt;();
static HashMap&lt;String, String&gt; UserAFavCol = new HashMap&lt;String, String&gt;();
    void addUser(String name, String pass, String col) {
        UserAPass.put(name, pass);
        UserAFavCol.put(name, col);
    }
}

答案4

得分: -1

public class User {
    public User(String name, String password, String favouriteColor) {
        this.name = name;
        this.password = password;
        this.favouriteColor = favouriteColor;
    }

    private String name;

    private String password;

    private String favouriteColor;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFavouriteColor() {
        return favouriteColor;
    }

    public void setFavouriteColor(String favouriteColor) {
        this.favouriteColor = favouriteColor;
    }

    public static void main(String[] args) {
        User users[] = {new User("Jason", "Sword", "red"), new User("Jack", "MyWord", "green")};
    }
}
英文:
public class User {
public User(String name, String password, String favouriteColor) {
    this.name = name;
    this.password = password;
    this.favouriteColor = favouriteColor;
}

private String name;

private String password;

private String favouriteColor;

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getFavouriteColor() {
    return favouriteColor;
}

public void setFavouriteColor(String favouriteColor) {
    this.favouriteColor = favouriteColor;
}

public static void main(String[] args) {
    User users[] = {new User(&quot;Jason&quot;, &quot;Sword&quot;, &quot;red&quot;), new User(&quot;Jack&quot;, &quot;MyWord&quot;, &quot;green&quot;)};
}

}

huangapple
  • 本文由 发表于 2020年9月14日 09:48:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63877139.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定