Array is a type of data structure which contains a collection of data
Different thant ArrayLists
Data in arrya can be primitive or referrenced
Int, float, char, vs String, array, class
Lowercase for primitive, uppercase for referenced
Element is value in a box of array, index is position of box, array length is total length of array
How to declare an array -Examples:
int[] array dom = new int [9]; (saying array length) or int[] array dom = {1,2,3,4,5}
//Unit 6 code example (for and enhanced for loop)
int [] values = {2,5,1};
int total = 0;
for(int i = 0; i < values.length; i++) { //adding values of an array through a for loop
total += values[i];
}
for(int element : values) { //adding values of an array through an enhanced for loop
total += element;
}
# [Unit 7](https://github.com/xXAASXx/FASTPAGES2/issues/12)
<img width="830" alt="Screen Shot 2022-12-15 at 9 31 23 AM" src="https://user-images.githubusercontent.com/89277945/207928469-13ae6200-e85e-4c74-bdcc-62e1db67ff2a.png">
- ArrayLists are like arrays, but can have vsize changed
- many methods which can be used with arrayLists
- size();
- Returns the number of elements in the list
- add(obj);
- Adds element at the end
- add(index, object);
- Adds element at specific index
- remove(index);
- Removes element from specific index
- set(index, object);
- Replaces element at index with new object
- get(index);
- Returns element at index
- can traverse arrayList with for-loop/enhanced for-loop
// Unit 7 code example
import java.util.ArrayList; //never forget to import
public class methodsArrayList {
public static void main (String[] args) {
ArrayList dogs = new ArrayList(Arrays.asList("Sparky", "Duke", "Noodle")); //remeber the <(Variable Type)>
ArrayList dogs2 = new ArrayList(Arrays.asList("Sparky", "Duke", "Noodle"));
System.out.println("There are " + dogs.size() + " in the ArrayList");
System.out.println("There are " + dogs2.size() + " in the ArrayList");
//objects you add must be of the same data type
dogs.add("Peanut"); //using arrayList functions
System.out.println("There are now " + dogs.size() + " dogs in the ArrayList");
String myDog = dogs.get(2);
System.out.println("My dog is named " + myDog);
}
}
//Note: you don't need to declare again after new ArrayList
public static void main(String[] args) {
String[][] arr = { // two indexes, to help store data in a 2D model
{ "Atlanta", "Baltimore", "Chicago" },
{ "Australia", "Boston", "Cincinnati" },
{ "Austin", "Beaumont", "Columbus" }
};
String longest = arr[0][0]; //defining the longest array at the moment
for(int row = 0; row < arr.length; row++) { //incrementing through each part to find largest string
for(int column = 0; column < arr[row].length; column++) {
if (arr[row][column].length() > longest.length()) {
longest = arr[row][column];
}
}
}
System.out.println(longest);
// Use nested for loops to find the longest or shortest string!
System.out.println("Use nested for loops to find the longest or shortest string!");
}
}
Test.main(null);
- 2D Arrays are a lot like arrays, but have an extra index which makes them 2D instead of 1D
# [Unit 9](https://github.com/xXAASXx/FASTPAGES2/issues/14)
- Inheritance is the order in which different attributes, methods, or strings are called
- depends on whentheer class is extended or not, if class contains methods, parameters, strings, etc
- constructor: Where the attributes of a class are defined
- Overriding: allows a subclass or child class to provide a specific implementation of a method that has already been provided by a super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature, and same return type (or sub-type) as a method in its super-class, then the method in the subclass will override the method in the super-class.
- Polymorphism: running mutliple things at once with the same name, can be done with different amounts of parameters in method
- toString method defines and JSONify's data protected is an access modifier so that the attribute isn't affected by outside modifiers.
- to use constructor of superclass in subclass, need to use super keyword, allowing use of constructors that were made in the superclass
```Example code...
// Demonstration of equals method
// Outputs boolean value of true or false
// If one object equals another
public class Student
{
private String name;
public Student(String name)
{
this.name = name;
}
public static void main(String[] args)
{
Student student1 = new Student("Bob");
Student student2 = new Student("Jeff");
Student student3 = student1;
Student student4 = new Student("A");
Student student5 = student4;
System.out.println(student1.equals(student2));
System.out.println(student2.equals(student3));
System.out.println(student1.equals(student3));
System.out.println(student3.equals(student4));
System.out.println(student3.equals(student4));
System.out.println(student5.equals(student4));
}
}
Student.main(null);
Recursion is when a method calls itself repeatedly to solve a problem
Contain two parts
base case: condition to be reached or returned when conditions are met
recursive call: the method being run over and over again
uses if and else statement mainly
Binary seach algorithm:
Data has to be in sorted order
Splits array in half multiple times until value is found
Selection Sort:
finds minimum element from unsorted part and puts it at end of sorted part
Merge Sort:
splits array into 2, calls it self into two sorted halves, and then merges all the havles back into arrayList
// Unit 10 recursion example
public int fact(int n) // a random number inputed, factorial of which wil be calculated
{
if (n == 1) // base case
return 1; //once n is found to be the same value as 1, the code stops
else
return n*fact(n-1); //if values not equal, then the function keeps calling the else part
}
Scores of All Homework Assignments Units 6-10!
Unit 6: 0.9/1
Unit 7: 0.8/1
Unit 8: 0.9/1
Unit 9: 0.7/1
Unit 10: 0.8/1
Unit 6
How to declare an array -Examples:
int[] array dom = new int [9]; (saying array length) or int[] array dom = {1,2,3,4,5}
for(int i = 0; i < values.length; i++) { //adding values of an array through a for loop total += values[i]; }
for(int element : values) { //adding values of an array through an enhanced for loop total += element; }
// Unit 7 code example import java.util.ArrayList; //never forget to import
public class methodsArrayList { public static void main (String[] args) { ArrayList dogs = new ArrayList(Arrays.asList("Sparky", "Duke", "Noodle")); //remeber the <(Variable Type)>
ArrayList dogs2 = new ArrayList(Arrays.asList("Sparky", "Duke", "Noodle"));
System.out.println("There are " + dogs.size() + " in the ArrayList");
System.out.println("There are " + dogs2.size() + " in the ArrayList");
}
//Note: you don't need to declare again after new ArrayList
methodsArrayList.main(null);
// Unit 8 code example public class Test {
} Test.main(null);
Unit 10