WEEK 7 PART 2 Flashcards
What is an Array?
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.
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. 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
What’s List? What’s the difference between List and Array?
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.
What’s the syntax of creating List and adding value in List?
Create: List name = new ArrayList();
Add: List Name.add(“ “);
Why do we need to covert Array to List?
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 to print an Array?
How to print a List?
System.out.println(“message….: “ + Arrays.toString(Array-Name));
System.out.println(“message….: “ + List-name);
How to covert List to Array?
Array-Name = array List-Name = list
array = list.toArray(new String[list.size()]);
How to convert an Array to List?
List List-Name = new ArrayList<>();
What’s indexOf? When do we use indexOf?
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.
What’s the syntax of indexOf searching from beginning or from certain index point?
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));
Whats Substring?
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
Length() method
Length method brings/returns the length of a string.
String str = “agdjdydbd”;
System.out.println(str.length());
Replace() method
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
Trim() method
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”
ChatAt() method
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