WEEK 7 PART 2 Flashcards

1
Q

What is an Array?

A

An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables.

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

A. What’s the syntax of writing an Array?

B. How can we find the length of an Array?

C.How to sort something in Array?

A

A. Data-type [] Array-Name;
Example: String [] states_name

B. Array-name.length;
Example: states_name.length;

C. Arrays.sort(states_name); –Ascending
Arrays.sort(states_name.Collections.reverseOrder); – Descending

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

What’s List? What’s the difference between List and Array?

A

List is one kind of Array but the biggest difference is List allows to change the value - add, remove or modify - without replacing existing value. To make changes in Array we would need the Array location of the variable to change that.

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

What’s the syntax of creating List and adding value in List?

A

Create: List name = new ArrayList();

Add: List Name.add(“ “);

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

Why do we need to covert Array to List?

A

Because any data manipulation in Array is very tedious and complicated job since we need the index no to do any manipulation in Array but it’s really easy in List. In List we don’t need to use index no, we can just enter the data we are trying to add or remove.

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

How to print an Array?

How to print a List?

A

System.out.println(“message….: “ + Arrays.toString(Array-Name));

System.out.println(“message….: “ + List-name);

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

How to covert List to Array?

A
Array-Name = array
List-Name = list

array = list.toArray(new String[list.size()]);

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

How to convert an Array to List?

A

List List-Name = new ArrayList<>();

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

What’s indexOf? When do we use indexOf?

A

ndexOf is used to find the position of the First occurrence of a certain character in a string. Even if the word/character exists multiple time, indexOf will find the position of the First occurrence.

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

What’s the syntax of indexOf searching from beginning or from certain index point?

A

System.out.println (string-Name.indexOf(“character”));

If we start the search from a certain position instead of the beginning, syntax is -

System.out.println (string-Name.indexOf(“character”, index-no/position));

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

Whats Substring?

A

Substring method finds/returns a part of the string. Substring returns the value from the Starting Index # to the end of the string.

Unlike indexOf, we need to use either - ‘Starting Index #’ or ‘Starting and Ending Index #’

String str = “JavaPoint”
String obj = str.substring(2); //vaPoint

String obj = str.substring(2, 6); //vaPo

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

Length() method

A

Length method brings/returns the length of a string.

String str = “agdjdydbd”;
System.out.println(str.length());

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

Replace() method

A

Replace method is used to replace a character in a string. It searches a string for a specific character and returns a new string after replacing those specific characters.

String str =”Hello World”;

Replace 'l' will be replaced by 'p'
System.out.println(str.replace('l', 'p'));
//Hello Worpd
Replace a word - World to Class
System.out.println(str.replaceAll("World", "Class"));
//Hello Class
Replace only first word - Hello to Hi
System.out.println(str.replaceFirst("Hello", "Hi"));
//Hi Class
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Trim() method

A

Trim method is used to trim or delete white spaces from beginning and ending of a string. It doesn’t change the original string.

String str = “ Hello”;
System.out.println(str.trim()); //”Hello”

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

ChatAt() method

A

CharAt returns the character from a specific location.

Difference between Substring and CharAt is CharAt only returns the location of a specific character whereas Substring returns from string from specific location to the end

String str = “Hello”;
char result = str.charAt(1);
System.out.println(result); //e

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

ValueOf() method

A

ValueOf method is used to convert any data type values to String values. Exam - int to string, char to string etc.

Int i = 30;
String s = String.valueOf(i);

17
Q

Format() method

A

Format is used to concatenate other type of value with string. This makes concatenate and coding more manageable and cleaner, specifically if we have to change value in the middle of the sentence.

%s - string, %c - character, %d int, %f - float etc

//My name is Jonathan and I am 20 years old and my salary is $40,000.

String name = “Jonathan”;
Int age = 20;
Int salary = 40000;

String s = String.format(My name is %s and I am %d years old and my salary is $%d, name, age, salary);

18
Q

Contains() method

A

Contains method checks whether a string contains a sequence of characters. It returns True if the characters exist and False if the characters don’t.

Main difference between contains and indexOf is contains returns true/false and indexOf returns the index no.

String str = “My name is John”;
System.out.println(str.contains(“John”)); //true
System.out.println(str.contains(“Jo”)); //true

It will also return true, even if it contains partial word.

19
Q

Split() method

A

Split method is used in order to separate individual words from a string using a deliminator. Split returns the result is Array.

Coma is an example of deliminator. Use whatever you have in the main code.

String names =”John, Ali, Abraham”;
String[] nameArray = names.split(“,”);
System.out.println(Arrays.toString(nameArray)); // [John, Ali, Abraham]

If you want to read these value individually, use for loop -
For(String objName: nameArray) {
System.out.println(“Individual name is : “ + objName);
}

//Individual name is : John

20
Q

How to get the Max value from an Array?

A

Int [] number = {20, 30, 40, 50, 60};

int i;
int max = number [0];
for (i = 1, i > number.length, i++) {
if (number [i] > max)
max = number [i];
}

21
Q

How to get the Min value from an Array?

A

Int [] number = {20, 30, 40, 50, 60};

int i;
int min = number [0];
for (i = 1, i > number.length, i++) {
if (number [i] < min)
min = number [i];
}