7: Array, but in KISJ Edition
Hey KISJ warriors! 🚀 Remember that spicy topic about the grading policy and how students suddenly became allergic to doing assignments unless there's an apparent threat of reassessment? Let’s dive into a new topic which is just as spicy (if not more)—Arrays in Java! You know, those things that are a bit like when STUCO gathers everyone for assembly, and tries to fit everyone's attention into a tight space? Yep, those!
What's an Array?
Arrays are basically like those school assemblies. A bunch of similar things (like us students) crammed into one space. But just like the school rule about trips to the deli, where you apparently need a chaperone (seriously, we're not 1st graders), arrays in Java come with their own rule: They ain’t flexible in size. Once you decide how big it’s going to be, it stays that way!
Starting Off with Arrays
Let’s consider our school. If we wanted to store everyone's student numbers, it'd go something like this:
int[] studentNumbers; // Declaring an array. This is like saying, "Hey, we need a space for student numbers!"
studentNumbers = new int[10]; // Allocating space for 10 student numbers. Imagine cramming 10 STUCO members on stage.
You know how some of us multitask during advisory? (Texting, cramming, or the classic ‘strategic eye-closing’). In Java, you can multitask in initializing arrays:
The Efficient One-Liner (because who has the time?)
dataType[] arrayRefVar = new dataType[arraySize];
The “I know who’s in my squad” Declaration:
dataType[] arrayRefVar = {value0, value1, …, valuek};
Array Techniques
Accessing and modifying elements in an array is like checking who sent you that latest meme during advisory:
studentNumbers[0] = 380; // Assigning a number to the first student.
System.out.println(studentNumbers[0]); // Revealing the number (or the meme source).
Got a list of memes and want to find the spiciest one? Arrays got you:
if (myList[i] > max) max = myList[i]; // Finding the dankest meme.
Arrays & Methods: Like Texting during Advisory
We've all shared memes in a group chat and realized that what's seen cannot be unseen. Similarly:
public static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
Passing an array to a method in Java is like sending a meme in a group chat. Once it’s there, everyone has access to it, and you can't really take it back without consequences. So, pro-tip: Think before you share (or modify arrays within methods)!
In summary, arrays in Java are like those classic KISJ moments. They’re a mix of rules, quirks, and trying to find the best way to fit everything in. And just like the “spicy” debate on assignments, they come with their set of challenges and fun. So, dive into Java, make some arrays, and maybe sneak in a meme or two. 😉 Happy coding! 🎉👩💻👨💻