How to create an arraylist class that can store multiple objects?

I'm self-taught. I am currently creating a GUI project in which I need a database of type matrice.

I would like to know how I can create a class that can store multiple objects in an arraylist.

Here is my sample code. Please note that this is just my attempt. This code is not complete and it does not work.

Thanks for your kind help.

import java.util.ArrayList; import java.util.List; 

}}

+4
source share
1 answer

I think the best way to do this is to create a custom information class to store information for a specific user like this.

 // I made them all public but this might not be a good idea! class UserInfo { String user; String pass; String secretCode; } 

And you put it in an ArrayList.

 ArrayList <UserInfo> InfoList = new ArrayList<UserInfo> (); 

Then for your current methods you can do

 // Not so sure what you want to do in this method... so you get to figure out that yourself! public void userInternalDatabase (UserInfo info) { this.user = info.user; this.pass = info.pass; this.secretCode = info.secretCode; } public void addUser(String i, String j, String k) { UserInfo newUser = new UserInfo(); newUser.user = i; newUser.pass = j; newUser.secretCode = k; InfoList.add(newUser); } public Object findUsername(String a) { for (int i=0; i <InfoList.size(); i++) { if (InfoList.get(i).user.equals(a)){ return "This user already exists in our database."; } } return "User is not founded."; // no Customer found with this ID; maybe throw an exception } 
+10
source

Source: https://habr.com/ru/post/1469399/


All Articles