Forum Posts

William Li
Mar 03, 2022
In Introduction to Java
In today's class, we learned about functions. In Java, which is an object-oriented language, all functions are methods inside an object. We can create a function by using reserved keywords and a function name, as well as arguments and a return type. When using functions, we have to be careful of the scope of the variables we're using. We also have to watch out for using arrays in our arguments - this is the difference between passing by value and passing by reference. When we use an array as a parameter in the function, whatever values we tweak inside will be changed in the array - if we have a method that doubles every integer in an int[], the actual values (int[0], int[1], etc.) will be changed. On the flip side, when we pass a primitive type the variable values won't change, as they are passed by value. To illustrate this, imagine a method that doubles an int. If we pass a variable i with a value of 6 into the method and then print it out after, the value will still be 6. https://replit.com/@offexe/functions/ We also learned how to write functions and the parts of a function, such as its scope and its return type.
0
1
3
William Li
Feb 10, 2022
In Introduction to Java
Today in class, we learned about parallel arrays. These are essentially two arrays that correspond to each other. For example, we can have an String[] employees array that corresponds to an int[] workHours array. These aren't related in a technical sense, so changing one array means that you must change the other one. We call them parallel because the first index of one corresponds to the first index of the other(s), the second one corresponds to the second, and so on. The issue with parallel arrays is that they aren't actually related, so it can be very easy to lose track of each array and the correspondence can be broken. We also learned about 3D dimensional arrays. We already know how to create 2D arrays, but can we go further? The answer is yes. To initialize a 3D array, we use the following syntax: dataType[][][] array = new dataType[x][y][z]; //where x, y, and z are different lengths of each "dimension" of an array (i.e. length, width, height) In Java, we can actually initialize arrays of any number of dimensions. However, it's hard to keep track of arrays that are more than three dimensions because we live in a three-dimensional world. Remember that 2D and 3D arrays are just arrays of arrays. This will help you use them more effectively and they will over time become less confusing. And since they are just arrays of arrays, we can make each array in the multi-dimensional array a different length. These are called ragged arrays. The simplest way to initialize these are to create the first array but with no columns: dataType[][] array = new dataType[x][]; //where x is the number of subarrays you want And then to initialize each of the arrays in the first array: array[0] = new dataType[1]; array[1] = new dataType[2]; array[2] = new dataType[3]; ... array[x-1] = new dataType[2]; An example of a use of a ragged array is a school, where each one has different classes of different sizes. The final concept we learned in class was copying arrays. When copying variables, we can just do the following: int x = 35; int y = x; and the integer y will be 35. When we try this with arrays, no error will pop up, but we will notice something strange when we change values in the original array: int[] array1 = {1, 2, 3, 4, 5}; int[] array2 = array1; array1[0] = 5; System.out.println(array2[0]); //this outputs 5 - why? So what happened? When we created the second array, we actually assigned the first array to the second - so when Java sees this, they think we are referring to the same array. In other words, array2 is just an alias for array1. In the memory, they both are associated with the same memory blocks. We can avoid this by using a for loop to copy arrays, or by using the Arrays.copyOf() method or the System.arraycopy utility.
0
2
5
William Li
Jan 27, 2022
In Introduction to Java
Today, we learned about: - Enhanced for loops These allow you to traverse an array without using an increment variable. For example: for (int i : myIntArr) { System.out.println(i); //prints out each int in the array in order } this would be equivalent to: for (int i = 0; i < myIntArr.length, i++) { System.out.println(myIntArr[i]); } note that in the first example, "i" is set to the actual value at that index in the array, while in the second example using a normal for loop i is the index at which an int is stored. The enhanced for loop only gets data, so you can't use it to set elements in an array. Nor can you traverse the array in any other order. We then practiced using enhanced for loops in some practice problems.
0
1
6
William Li
Jan 13, 2022
In Introduction to Java
An array is a type of data structure. It is simply a collection of a same type of data. For example, an int array could store some number of the primitive data type int. To reference a specific variable array, we use indexes as follows: array[index] Where index is the index of the variable in the array. Note that arrays start at index 0, i.e. the first element is array[0]. You can declare an array in the following ways: - Manually setting every value: dataType[] array = {datatype1, datatype2, datatype3}; //where the datatype variables represent elements of the data type of the array - Creating an empty array of a set length: dataType[] array = new dataType[3]; //creates 3 spaces (0, 1, 2) of the default value of the data type specified Arrays are immutable, so their length cannot be changed once it is initialized. To find the length of an array, simply use arrayName.length. However, the indexed variables could be mutable, so they can be changed.
0
1
4
William Li
Dec 16, 2021
In Introduction to Java
A series is a continuing sum of terms that have a pattern. It can be very useful to know how to code a program to computer a series. For example, we can add up a series of numbers using the following code: int sum = 0; for(int i = 0; i < 10; i++) { sum += i+1; } //finds the sum of the first 10 whole numbers (1, 2, 3...) We can also use it to find a more complicated series: //find the sum of the series 1+1/2+1/4+1/8+1/16+1/32... to the 10th term for(int i = 0; i < 10; i*=2) { sum += 1/(i^2+1); } We also have a handy loop known as a while loop. This runs until a condition is false. while(condition) { doSomething(); } if the condition will always be true, then the loop may run infinitely. We then went over lots of practice problems involving loops.
0
2
9
William Li
More actions