// Here are some classic java import statements, whenever you // are trying to use a java object (ex. List or Scanner) you will // need to first import that object import java.util.*; import java.io.*; // To make a class you have two keywords // public : which means that the class is accesible to all other classes // class: this indicates a class // followed by a name, this name will also have to be the name of the file public class ArrayIntList { // Anytime you are making a data structure, you need to store data // this data is stored in global variables called fields in java // we want all our fields of a public class to be private private int[] arr; private int size; // to build a class you create constructors // java allows for multiple constructors, as long as each // constructor has different parameters public ArrayIntList() { // we use the keyword "this" to call another constructor // in the class, for instance here we are calling the second // constructor that takes in an int parameter this(100); } public ArrayIntList(int cap) { // fields are automatically initialized to the java null // (or null equivalent), in the constructor we initialize these // fields to something this.arr = new int[cap]; // to reference a field, I can use the "this" keyword followed // by a period. However, this is not neccessary as long as there // are no other variables in local scope with the same name as your field this.size = 0; } // Our method headers contain 4 parts // 1. "public" or "private" depending on whether or not the method should // be allowed to be accessed from outside classes // 2. a return keyword, for instance in this method we used the keyword // "void" to mean that no value is returned. // If we wanted to return a different value, like an int we would // replace the word "void" with the word "int" // 3. a name of the method, if you want a name to have more than one word, // like addAtIndex, the name must be camelCased // 4. parameters, which will be a type of value followed by a name, separated by // commas. In java there are not lambda parameters or optional parameters // This method adds a new value at an index public void add(int index, int value) { if (index < 0 || index > this.size) { // one way of error handeling is to throw exceptions // to throw an exception you use the two keywords // "throw" "new" then the name of the exception followed by () // do not write your own custom exceptions for this class throw new IllegalArgumentException(); } size++; int temp = this.arr[index]; this.arr[index] = value; // there are several types of loops that will be useful for you in // java, for loop (shown here), while loop, and for each loop // In a for loop we initialize a variable, give the stopping condition, // and then the update all seperated by semi-colons for (int i = index + 1; i < size; i++) { int temp2 = this.arr[i]; this.arr[i] = temp; temp = temp2; } } // writing your own toString is a great way to help debug your classes // if you want to name your method "toString" you must take in no parameters // and return a string // this toString method doesn't work very well, because it prints a bunch of 0s // after our actual list, however it works well enough for the purposes of our // testing program public String toString() { return (Arrays.toString(this.arr)); } }