Intro to FRC Programming - Romi
  • INTRODUCTION
    • Overview
    • Important Links
  • 💽Setup
    • Setting up the Romi's Software
    • Downloading Essential Tools
    • APCS vs FRC
  • How To Code in VSC
    • VSC- Intro
    • A Tour Through VSC
    • Creating a Regular Java Project
  • Intro to Java
    • What is Java?
    • Beginning Steps
    • 🟩Fundimentals of Java
      • Variables
      • Operations
      • Methods
      • Comments
      • If Statements and Conditions
      • Boolean Zen
      • Loops
      • Challenge- Create a Box House
    • 🟨Advanced Concepts
      • Objects
      • Scanners
      • Null Objects
      • Arrays
      • Errors
      • For-Each Loops
    • 🟥Object Oriented Programming
      • Basics of OOP
      • Instance Classes
      • Static Classes
  • 🕑Romi Curriculum- Timed Base
    • Romi Curriculum- Introduction
    • Creating a WPILIB Project
    • Running Your Code
    • The Robot Class
    • Subsystems
      • RomiDrivetrain
    • Cool stuff i will rename this category later
      • Spark Motors
      • PIDs
      • External Controllers
      • Encoders
  • 🖥️Romi Curriculum- Command Based
    • Command Based Code
    • RobotContainer
    • Commands
    • CommandScheduler
  • UNRELATED IMPORTANT CODING KNOWLEDGE
    • Constants
  • SAMPLE CODE
    • Tank Drive Example
      • RobotContainer
      • TankDriveSubsystem
      • MoveRobotCommand
    • Worktop Systems Sample Java Code
      • Belt Elevator Sample Code
      • Rotating Arm Sample Code
Powered by GitBook
On this page
  • Parts of an Array
  • Creating an Array
  • Getting Values of an Array
  • Modifying Arrays
  • Challenge— Fill up the Fruits array
  1. Intro to Java
  2. Advanced Concepts

Arrays

Arrays are collections that can hold multiple values of the same type in a single variable. They are complicated but very powerful. As in they are so powerful that a single array can store every single letter of the Shrek script.

Parts of an Array

Each item of an array is called an element. Other than being stored in an array, elements are essentially variables.

Each element has an index, which is an int that refers to the element's location in an array. Think of it as its address.

In Java, the index begins at 0 for the first element. This is called zero-based indexing.

An array is like a complex of storage units. An element can reside in each storage unit, but the storage units can also be empty. Each row of the complex contains a pre-determined number of storage units, the unit numbers beginning at zero.

Creating an Array

Let's say you wanted to make an array to store the names of fruits because you're such an avid fruit enjoyer. But how would we create an array? This is one way:

These are the syntaxes for creating an array:

type[] arrayName = new type[lengthOfArray];
type[] arrayName = {element, element2, element3, etc.}; /* this is only if you know 
                                                         * exactly what you're putting
                                                         * in the array */

However, we'll focus on the first syntax for now. Using it, you can create this array:

String[] fruits = new String[5]; // creates an array with five elements

Now we have an array! Arrays can only have a set amount of elements, which we just declared to be 5 (The number in the square brackets). Think back to the analogy of the storage units. Now, there are five total storage units in the row. However, they are all empty.

Getting Values of an Array

However, we have a problem. Although we have five elements, we didn't assign a value to any of them. To make sure that the array actually has five values, it automatically fills out these empty elements to null. (If you try to interact with a null object, like doing math on it, it'll cause an error).

If we go back to the neighborhood analogy, it's as if the entire neighborhood is filled with empty plots of land dedicated to houses.

This is what the array looks like right now:

{null, null, null, null, null}

You might wonder how we'd access the elements of an array. To refer to an element of an array, we'll just assign a variable this syntax:

arrayName[index of object]

This is just a pointer to the element at the given index. We can treat it like how you would with a variable. So, back in the fruits array, if we wanted to access the second element of the array, we'd call fruits[1].

String fruit = fruits[1];
System.out.println(fruit); /* error because fruits[1] is null */

Modifying Arrays

Since we're filling up our array with fruits, we'll have to manually replace each of these null elements one by one.

As I mentioned, arrayName[index] will give a pointer to the element at said index. This means that if we want to set the second element of fruits to something, we could just do this:

fruits[1] = "Apples";

If we tried printing it, the compiler wouldn't writhe and scream in pain!

String fruit = fruits[1];
System.out.println(fruit); /* output: Apples */

What if you tried to call something outside of the array's range? Your compiler will give you the dreaded ArrayIndexOutOfBoundsException error— a Runtime Error. If you want more information on them, look into Errors.

Challenge— Fill up the Fruits array

Since I'm too exhausted to fill out the rest of the array thinking of a semi-decent example, you can do it for me!

All you'll need to do is:

Solution
/* one solution */
String[] fruits = new String[5];
fruits[0] = "Strawberries";
fruits[1] = "Apples";
fruits[2] = "Pumpkins"; // pumpkins are fruits btw
fruits[3] "Oranges";
fruits[4] = "Cherries";

/* alternate one-line way */
String[] fruits = {"Strawberries", "Apples", "Pumpkins", "Oranges", "Cherries"};

PreviousNull ObjectsNextErrors

Last updated 2 months ago

🟨