ArrayLists

ArrayLists are containers (like arrays, lists, etc), which store data within them. Unlike traditional arrays, ArrayList sizes are not set and can be modified. In Java, they act as objects with their own special syntax. Instantiating an ArrayList is almost essentially the same with any other object where the user needs to call a constructor. In addition, Java's strong type makes lists, unlike in weak type languages like Python, all the same data type. Overall, ArrayLists are very useful, especially in the context of data bases, because they are expandable containers of data that we can access in a variety of different manners.

Below is an implementation of ArrayLists with a rough application to our Science Olympiad website.

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.isEmpty()){ //isEmpty() method is useful if we need to check for sure that data is clear or if we want certain conditions
            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;
        }
    }
}

Let's See ArrayLists in Action

ArrayLists can have any type of data in them ranging from primitives (booleans, int, double) to wrapper classes and any other type of object. Below is a member list of Science Olympiad and here I use a variety of methods that can alter or retrieve the data within the ArrayList. The code includes comments explaining the particular use case and why it's useful.

public class Main{
    public static void main(String[] args){
        //creates memberList and prints members; some uses of methods within print method
        ArrayList<Member> memberList = Member.createMemberList();
        Member.printMembers(memberList, "instantiating memberList array; get(); size(); isEmpty(); attribute specification");

        //add(element) method; Useful for adding new members to databases and additional relevant info
        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"}));
        Member.printMembers(memberList, "add()");

        //clear() method; Useful for clearing members every new season from current competition databases
        memberList.clear();
        Member.printMembers(memberList, "clear()");
        memberList = Member.createMemberList();

        //remove(int index); Useful for removing members at a specific index in case someone drops out
        memberList.remove(1);
        Member.printMembers(memberList, "remove(int i)");

        //remove(element); not sure how it could be practically used in reference to objects but it could be useful to remove items from our database according to a particular value of data

        //set(int i, element); useful for overwriting data in potential cases of a member or merging data of accounts?
        memberList.set(1, new Member(11, "Gaik", "Gaiko@gmail.com", "900-900-9000", "34562", 10, "deez", new String[]{"Slick", "Smart", "Speaker"}));
        Member.printMembers(memberList, "remove(int i)");

        //indexOf(element); Might be useful if can be easily accessed and indexed by element w/out having to recreate each time, might be useful for finding certain users
        //memberList.get(i).indexOf(member); (wrong lol)

        //lastIndexOf(element); Same as indexOF
        //memberList.get(i).lastIndexOf(member); (wrong lol)

        //equals(); same reason it would be hard to use as s indexOf but would be useful for checking if member accounts are the same or if any particular data is similar
        //memberList.get(i).equals(member); 

        //hashCode(element); Perhaps useful for figuring out certain information in reference to where the objects are being stored in memory or when we need to reference an object
        //memberList.get(i).hashCode(member);

        //contains(element); Same as indexOf except in this case checking if certain users are there or not
        //memberList.contains(memberList.get(i));   

        //containsAll(Collection collection); Useful for checking if a group of certain users, similar impracticality as indexOf
        //memberList.containsAll(memberList.get(i));
        
        //sort(Comparator comp) Useful for sorting/organizing certain groups, in particular, likely for local client interactions when admins need to sort data
        //memberList.sort();
    }
}

Main.main(null);
----------------------------
Method: instantiating memberList array; get(); size(); isEmpty(); attribute specification
Your Mom
Joe
Jack
Jill
----------------------------
----------------------------
Method: add()
Your Mom
Joe
Jack
Jill
Morb
----------------------------
----------------------------
Method: clear()
no one here :(
----------------------------
----------------------------
Method: remove(int i)
Your Mom
Jack
Jill
----------------------------
----------------------------
Method: remove(int i)
Your Mom
Gaik
Jill
----------------------------