Loading...

Postulate is the best way to take and share notes for classes, research, and other learning.

More info

Java Arraylists and Arrays

Profile picture of Laura GaoLaura Gao
May 18, 2021Last updated Apr 30, 20223 min read

Following my prestine history of superb decision making, I'm going to make the awesome decision to keep track of my crushes in Java. Using an array seems like a good candidate:

String[] crushes = {"prince harry", "thanos"};

// yes, actually. Java uses curly braces for array declaration instead of square brackets.

Arrays let me find out what my 69th crush was:

System.out.println(crushes[68]); // given the array has 69+ elements

And arrays will tell me how many times I changed my mind:

System.out.println(crushes.length);

// note: not crushes.length()

But my interests are fleeting and soon, I will have new crushes. but Java arrays don't let me add new crushes!

Turns out, I wasn't the only one with this problem. Turns out, legit people who do legit things with Java like store legit information need to add elements to their arrays too.




And thus, the arraylist was born.

Now, I can do all sorts of new things with my crushes! I can add new crushes whenever my heart desires:

crushes.add("hulk");

I can print out all my crushes with one line of code as opposed to having to loop through each element:

System.out.println(crushes);

I can change who my 69th crush is, if I want to reserve that special spot for someone else ;)

crushes.set(68, "amy li");  // given the array has 69+ elements

After a tough breakup, I want to forget that I loved you in the first place. Arraylist allows me to I can delete you from my records.

cars.remove(68); // remove method to remove an element based on index number

I can even delete all of my records if I don't wnat my parents to find out that I was spending time on making crushes arrays instead of practising legit CS.

crushes.clear();

But all these perks come with a few changes. Firstly, I have to declare my crushes variable with this funky syntax:

ArrayList<String> crushes = new ArrayList<String>();

I can't directly add values to arraylist on declaration, so I'll have to use crushes.add() to add each crush. (There is a workaround though)

To find the number of crushes, arraylists use a different method.

crushes.size(); // as opposed to crushes.length

And to get a specific element, arraylists also use a different method:

crushes.get(68); // as opposed to crushes[68]

But minor inconveniences aside, arraylists allow me to do everything I want with my crushes! Hip hip hurray!



I wrote about arrays and arraylists before in my mega Java syntax guide, but it was too condensed and I forgot a ton of arraylist methods when doing practice AP CS tests. I'd even forgotten that I'd written about them. So I'm taking notes again in a different (hopefully more humorous) method to drill them in my head. Don't make any arraylist syntax errors on the test.

Neat source of arraylist methods


Comments (loading...)

Sign in to comment

Dev/CS

Builders gonna build baby