Java Flashcards
How to start a one-dimensional array
int [] a = new int [//limit eg 3//]
How do you print an entire array?
for (int i:a) {
System.out.println(i);
When should you use a method?
When you reuse a set of code throughout the program, you can rewrite it in a way that is better for efficiency and saving space.
What is the structure of a method?
<access> [static] <return> <camelCaseName(<parameter>).
</parameter></return></access>
What the hell is an access modifier?
There are 2 main types
< private>
< public >
Private if you don’t want the program to know the output unless you manually refer back to the camelCaseName(); which acts like a caller off the method. That where you resuse
The public is otherwise.
What is a return data type?
Every method, unless void, must return something.
This is basically the type of data you want the method to return, whether a boolean, strings, or integers.
What is a parameter list in a method?
Separated by commas, that gives the definition of the data that are going to get in, for eg, int a, int b.
These are limited, so if you add 2 parameters, it will only perform using 2 inputs; otherwise, it will give an error.
What is a record in Java
Similar to a tuple in Python. idk what the hell that is. There is such a difference, but it allows you to do something similar to a method but more advanced. instead of allowing one data type, it allows more and also returns the value of the variable assigned.
<access> record <Name>(<data> <variable>[, <type> <variable>]){}
</variable></type></variable></data></Name></access>
How do i repeat a code of the user input?
for (int i = 1; i <= n; i++) {
This means int i =1, meaning it starts from 1
i <= n, continues until i is greater or equal to n (user input)
Every time it runs the code, it i++
meaning
i = 1+i
it increases by one until it equals n.
What does x += y mean in Java?
It is the same as x = x + y
waht does <add(int index, E element)> do?
adds element at position index. If this is before the current end of the list, it shifts everything over so you don’t overwrite anything.
what does <contains(Object o)> do?
returns true if o is in the list. As every class inherits from Object, this works for any object (yes, this gets confusing when we starting using Object as the name of a class as well as for objects, just pay attention to the capitalisation and the font [and context]).
what does <add(E element)> do ?
adds element to the end of the list, wherever that is. E is the type of the element - we’ll get to that in a slide or two.
waht does get(int index) do?
returns the element at index index.
what does indexOf(Object o) do?
Returns the index (as an int) of o if it’s in the list, -1 if it’s not in there at all.
what does < remove(int index)> do?
removes the element at index index and returns it, shifting things over to fill the gap if necessary.
what does < remove(Object o)> do?
removes the first occurrence of o from the list, shifting things over to fill the gap if necessary. It returns true if the operation actually did something - i.e. if it found o.
what does set <(int index, E element)> do
Replaces the element at index with element (no shifting).
what does <size()> do?
Returns the number of elements in the list.
How to use ArrayList?
import: import.java.util.ArrayList
declaration :ArrayList<Integer> list = new ArrayList<Integer>();</Integer></Integer>