1/23
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Reminder:
8 Primitive types
4 Decimal numbers (byte, short, int, long)
2 Floating Point (float, double)
char
boolean
Reminder:
Everything else is a complex type
generally theyre Objects built upon primitive ← types
String “…”
String color = “blue”;
Complex data type
list of characters
data type is String with capital “S”
Strings are “stored” inside _____
Objects
Objects can contain ___
Data attributes
Methods (functions)
To access an attribute or call a method inside an object ___
you can use the . operator
ex:
String myColor=”Blue”;
int sizeOfColor=myColor.length()
Java String Manipulation:
•example.toUpperCase() → “TEST,1,2”
•example.toLowerCase() → “test,1,2”
•example.trim() //Removes spaces at front and end
•example.equals(String) → Compare strings
example.equalsIgnoreCase(String) → case insensitive compare
Accessing specific characters in a string
String example=”Test,1,2”;
●example.length() → 8 //There are 8 characters
●example.charAt(0) -> ‘T’ //The first character.
●example.charAt(example.length()-1) -> ‘2’ //Last character
●example.substring(2,5) → “st,”
○Note in Java it’s start position, end position, it doesn’t include the end position.
●example.split(“,”) //returns an array of strings [“Test”,”1”,”2”]
●example.toCharArray() //Produces an array of chars
Collections:
In Python you had lists []’s, tuples ()’s and sets {}’s
In Java you have:
Array[]
Arrays are fixed in size. You declare the size when you create them.
They can only hold one type at a time.
ArrayList
They grow and shrink as you need them to.
They likewise can only hold a single type at a time, you declare it when you create them.
ArrayList in Java Example
import java.util.ArrayList;
ArrayList<String> names = new ArrayList<String>();
names.add("alice");
names.add("bob");
names.add("eve");
for(String person : names) {
System.out.println(person);
}
ArrayList are…
●Linear data structure which can hold data of the same type
●As items are added it’s grows to the necessary size
●As items are removed it can shrink to the correct size
●Like arrays, they are indexed from position 0
●Much like a list in Python
Creating an ArrayList in Java
import java.util.ArrayList;
ArrayList<String> myStr = new ArrayList<String>();
ArrayList<Integer> myNum = new ArrayList<Integer>();
ArrayList Notes:
You specify the type you will store in the ArrayList in <>’s
ArrayList can only hold items of a single type.
ArrayLists must be of OBJECTS. If you want an arraylist of primitive types you must use the object wrapper (int -> Integer, double -> Double etc).
ArrayList Methods
Note: they start with a size of 10 and grow as needed
○.add()
○.remove(index to remove)
○.remove(object to remove)
○.size() //returns how many items in the ArrayList
○.get() //returns object at index
○.clear() //clears the ArrayList
○.contains() //Searches if an item is in the list
○.isEmpty() //returns true/false
○.set(index, new) //replaces the item at position index
ArrayList Methods Example:
import java.util.*;
class Main {
public static void main(String[] args) {
ArrayList<Integer> myList = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
myList.add(i);
}
for (int x : myList) {
System.out.println(x);
}
}
}
Arrays
●Arrays are very similar to ArrayLists, but are more limited.
○Cannot be sliced
○Can only hold a single data type
○Cannot change size once created.
●Think of them as a liner collection of one data type
●What is the benefit of Arrays?
○They are guaranteed to occupy the amount of space you allocated when you created them.
○They can be multidimensional.
Reading and writing “to” arrays
●Getting information from an array:
variable = name[position]; // reads value at position in name
●Storing information in an array:
name[position] = value; // stores value at position in name
●Note arrays are indexed from 0
○This is probably one of the most common mistakes when using arrays
○If you create an array of size 10, it has cells 0-9, NOT 1-10.
Alternative syntax to create and initialize Arrays
double[] grades = new double[] {92.8,78.4,44.6};
double sum=0;
for(int i=0;i<grades.length;i++) {
sum+=grades[i];
}
double average=sum/grades.length;
System.out.println("Average: "+average);
Resizing array?
WRONG:
create new one and copy over data
HOWEVER:
We can determine the size of an array by using .length
Searching through Arrays:
If you want to check if a particular element is inside an array, you’ll need to do it manually.
There is no “in” keyword for a “if element in array:” statement.
for(int i =0; i< array.length; i++){
if(array[i] == targetNumber){
System.out.println("Found at position " + i);
break;
}
}
Printing arrays
simply using System.out.println(array); only prints out the memory representation
if you want to print out array contents you need to →
int [] array = {1,2,3,4,5};
String output = "[";
for(int i=0;i<array.length;i++){
output+= array[i];
if(i+1 != array.length){
output+= ", "
}
}
output += "]";
System.out.println(output);
Comparing Arrays
simply using == operator WONT work
if(array2.length != array.length){
System.out.println("Arrays are not the same");
}
else{
boolean equals = true;
for(int i=0;..;...){
if (array2[i] != array[i]){
equals = false;
}
}
}
Slicing arrays:
If you want to slice an array, you’ll do it manually
The examples are equivalent:
Because the Array Class is an object, you can use some of Java’s built in “object” methods
Multi-Dimensional Array of Integers
int[][] myIntArray = new int[10][10];
This produces a 2D array with 100 cells.
You can think of it as an array of arrays.
The first number represents the row
The second number represents the column
You access the last cell as:
myIntArray[9][9];
You can have 3D, 4D arrays...
Working with multidimensional arrays
●This creates a 3x3 array
●Notice you’ll have 2 loops to iterate across the structure, one for rows, one for columns.