简体   繁体   中英

How to store data in arraylist in form of rows and columns

I am having an option to add new rows to add student details.

<table>
Name:<tr><td><input type="text" name="sname"></td></tr>
Id:<tr><td><input type="text" name="sid></td></tr>
</table>

<input type="button value="Add new row">

Now, I want to store all rows data in array list . How to store the data in array list in multiple rows like this ?

array_list:{["Pooja",1]["Priya,2]}

Make Student class having fields Name & ID. And store this object into ArrayList.

public class Student {

    private String name;

    private int id;

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

    @Override
    public String toString() {
        return String.format("[\"%s\", %s]", this.name, this.id);
    }
}

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

students.add(new Student(1, "Pooja"));
students.add(new Student(2, "Priya"));

// {["Pooja", 1],["Priya", 2]}     
System.out.print(students.stream().map(Object::toString).collect(Collectors.joining(",", "{", "}")));

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