简体   繁体   中英

Java: Storing an Array into an Array?

Is it possible to store three string values added into an array (studentName), and store that into a different array so it can be found later?

Basically my main goal is to store a name, user id, and a balance (fullName, idName, 300).

And add that into a "super(?)" array so when people type down, it finds the fullName and pulls the information from there.

You can create a class

public class Student {
    private String name;
    private String id;
    private int balance;
}

and then you can create a list of these objects:

List<Student> list = new ArrayList<Student>();

Map<String, String> map = new HashMap<String, String>();

then:

List<Map<String, String>> listOfMaps = new ArrayList<Map<String, String>>();

and then:

map.put("name", "Thomas");
map.put("id", "Thomas id");
map.put("balance", ""300);
listOfMaps.add(map);

Anyhow, be careful. You will have to keep numbers (fe balance) as a String and after you will need to map it.

Well, I believe you are talking about something like Jagged Array which is available in C# but for java, we can do it in some other ways... like creating a class and manipulating it as Generic List implementation...

public class Student {

private String name;
private int id;
private int balanace;

public Student(){}

public Student(String name, int id, int balance){
    this.name = name;
    this.id = id;
    this.balanace = balance;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getId() {
    return id;
}
public void setId(int id) {
    this.id = id;
}
public int getBalanace() {
    return balanace;
}
public void setBalanace(int balanace) {
    this.balanace = balanace;
}

}

In some other class where you would want to manipulate

public class ManipulateData {

public static void main(String[] args){
    Student student1 = new Student("James", 1, 500);
    List<Student> list = new ArrayList<Student>();
    list.add(student1);

    for(Student s: list){
        System.out.println("Name : " + s.getName());
        System.out.println("ID : " + s.getId());
        System.out.println("Balance : " + s.getBalanace());
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM