Java Flashcards

1
Q

How to start a one-dimensional array

A

int [] a = new int [//limit eg 3//]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you print an entire array?

A

for (int i:a) {
System.out.println(i);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When should you use a method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the structure of a method?

A

<access> [static] <return> <camelCaseName(<parameter>).
</parameter></return></access>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What the hell is an access modifier?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a return data type?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a parameter list in a method?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a record in Java

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do i repeat a code of the user input?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does x += y mean in Java?

A

It is the same as x = x + y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

waht does <add(int index, E element)> do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what does <contains(Object o)> do?

A

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]).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what does <add(E element)> do ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

waht does get(int index) do?

A

returns the element at index index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does indexOf(Object o) do?

A

Returns the index (as an int) of o if it’s in the list, -1 if it’s not in there at all.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

what does < remove(int index)> do?

A

removes the element at index index and returns it, shifting things over to fill the gap if necessary.

17
Q

what does < remove(Object o)> do?

A

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.

18
Q

what does set <(int index, E element)> do

A

Replaces the element at index with element (no shifting).

19
Q

what does <size()> do?

A

Returns the number of elements in the list.

20
Q

How to use ArrayList?

A

import: import.java.util.ArrayList

declaration :ArrayList<Integer> list = new ArrayList<Integer>();</Integer></Integer>