Arrays And Methods Flashcards
What are Primitive Data Types?
Static - set amount of memory, actual valve stored in memory location, pass by value
ex: int, double, boolean, float, etc
What are Object Type variables?
Reference Type, memory set aside varies, pass by reference
ex: String, Array
What is an array?
- Data structure that can hold multiple valves of the same type (advantage)
- can store primitive and non primitive data types (advantage)
- faster, takes less memory (advantage)
- fixed size - static (disadvantage)
- has less buit in methods (than array list) (disadvantage)
- Initializing: int[] list = new int [10];
What is a 2D Array?
- an array of arrays (ex: chessboard)
- declaring: int [] [] array = new int [4] [3];
What is an Arraylist?
- a class in java that can hold any type of object (No Primitive data types - boolean, char, int)
- can grow or shrink while program is running (advantage)
- has additional methods that are helpful (advantage)
- more flexibility and more dynamic (advantage)
- slower due to memory demands (disadvantage)
How do we declare an Arraylist?
1) import jan package: import java.util.Array List;
2) ArrayList < Type> arrayName = new ArrayList < Type> ();
How do we declare an Arraylist?
1) import jan package: import java.util.Array List;
2) ArrayList < Type> arrayName = new ArrayList < Type> ();
What is Method Overloading?
When there are two or more methods within the same class that share the same name but different parameters
What are methods?
- breaks code to smaller parts (advantage)
- easier to debug (advantage)
- more efficient and organized (advantage)
- can reuse code (advantage)
- best used to separate calculations, check data, etc
What are the types of methods?
- Functional - return type - executes code, then returns a value
- Procedural - void type - executes code, but doesn’t return value
Parts to a method
sum = getSum (num1 , num2); – argument
public static int getSum (int num1, int num2) { – modifiers, return type, method name, parameters
What is scope of variables?
- part of program that variables can be accessed from
- variables can only be accessed from the block it’s in
- ex: variable in a method can be used throught method but not in other methods
- variables cannot be accessed before they are declared
- Local variables cannot be accessed outside of the method
- variables defined in a block can only be accessed within the block
What is Pass by Value?
- When a copy of variable is sent to the method
- if value of variable is changed in the method there is no effect on the original copy, because they are discarded after exiting the method
What is Pass by Reference?
- When a memory location of the value is sent to the method
- this changes the values of the reference because they are not copies
- ex: array
Similarities and Differences between Bubble Sort and Improved
Bubble Sort: uses rested for loops , best case # of comparasions: n (n-1) / 2 , best case time: O (n^2)
Similarities: Worst case for # of comparisons: n (n-1) / 2, Worst case for time: O (n^2)
Improved: uses boolean variable, uses while condition, more efficient, Best # case: n- I, Best time case: O (n)