Arraylists Notebook
Captures Key Learnings ArrayLists
import java.util.*;
public class Member{
//instantiates attributes
static int idCounter = 0;
int id;
int grade;
String name;
String email;
String phoneNumber;
String password;
int yearsInSciOly;
String team;
String[] events;
//Constructor for each attribute
public Member(int grade, String name, String email, String phoneNumber, String password, int yearsInSciOly, String team, String[] events){
this.id = idCounter;
idCounter++;
this.grade = grade;
this.name = name;
this.email = email;
this.phoneNumber = phoneNumber;
this.password = password;
this.yearsInSciOly = yearsInSciOly;
this.team = team;
this.events = events;
}
//creates general ArrayList using Member objects
public static ArrayList<Member> createMemberList(){
Member member1 = new Member(11, "Your Mom", "mom.your@gmail.com", "696-696-9696", "12345", 5, "deez", new String[]{"Fermi", "Astrology", "your mom"});
Member member2 = new Member(12, "Joe", "joe.your@gmail.com", "958-696-9696", "123456", 5, "deez", new String[]{"Fermi", "joe star", "your mom"});
Member member3 = new Member(9, "Jack", "jack.your@gmail.com", "542-696-9696", "23456", 5, "deez", new String[]{"Fermi", "Niki", "How to Cope"});
Member member4 = new Member(10, "Jill", "jill.your@gmail.com", "234-696-9696", "123456", 5, "deez",new String[]{"Fermi", "Chem Lab", "Krish Patil"});
ArrayList<Member> memberList = new ArrayList<>();
//add() method adding members; useful for adding new members every time new member object instantiated
memberList.add(member1);
memberList.add(member2);
memberList.add(member3);
memberList.add(member4);
return memberList;
}
//print method that prints the name attribute of each object, prints method being used
public static void printMembers(ArrayList<Member> memberList, String arrayListMethod){
System.out.println("----------------------------");
System.out.println("Method: " + arrayListMethod);
//size() method gets integer length of memberList; useful for keeping track of number of members
for (int i = 0; i < memberList.size(); i++){
//get() method returns the object specified by index; useful for getting data of individual members
//.name retrieves the name attribute from the Member object
System.out.println(memberList.get(i).name);
}
if (memberList.size() == 0){
System.out.println("no one here :(");
}
System.out.println("----------------------------");
}
//Simple log in system that returns true or false (yes it is terribly unsecure)
public boolean login(String input){
if (input.equals(password)){
return true;
}else{
return false;
}
}
//main method
public static void main(String[] args){
//creates memberList and prints members; some uses of methods within print method
ArrayList<Member> memberList = createMemberList();
printMembers(memberList, "instantiating memberList array; get(); size(); attribute specification");
//clear method; Useful for clearing members every new season from current competition databases
memberList.clear();
printMembers(memberList, "clear()");
memberList = createMemberList();
}
}
Member.main(null);
public class Main{
public static void main(){
ArrayList<Member> memberList = Member.createMemberList(); //creates list of Member objects
memberList.add(new Member(11, "Morb", "tehee.your@gmail.com", "754-696-9696", "34542", 6, "deez", new String[]{"Green Generation", "Anime", "How to Hit Man"}));
}
}