mirror of
https://gitlab.com/magicalsoup/Highschool.git
synced 2025-02-02 12:51:46 -05:00
Update Array Manipulation Notes.md
This commit is contained in:
parent
a0900d0f75
commit
cf47430de5
@ -20,7 +20,7 @@ String array[] = {"s", "d", "f"};
|
||||
|
||||
## String Manipulataion
|
||||
|
||||
- ```length()` to find the length of a string
|
||||
- `length()` to find the length of a string
|
||||
- `stringName.charAt(0)` will output the first character
|
||||
- Each character in a string has a positio. String positions start at 0
|
||||
- `stringName.indexOf("T")` will output the index number that the letter `T` belongs to
|
||||
@ -28,16 +28,81 @@ String array[] = {"s", "d", "f"};
|
||||
- Eg. `string stringName = "Jim hates math"`
|
||||
- Output: `hates math`
|
||||
|
||||
- First # is for the index that you want and last number is for the index that you don't wnat
|
||||
- First # is for the index that you want and last number is for the index that you don't want
|
||||
- `stringName2 = " i hate math "`
|
||||
- `stringName2.trim();
|
||||
- output: i hate math"`
|
||||
- `stringName2.trim();`
|
||||
- Output: `i hate math`
|
||||
- `stringName.toLowerCase()` makes every character a lower case letter
|
||||
- `stringName.toUpperCase()` makes every character an upper case letter
|
||||
|
||||
- concat()
|
||||
- `concat()`
|
||||
- `stringname1.concat(stringname2);` will put both string into 1 word
|
||||
- `stringname.replce("a", "p");` will replace every `'a'` with `'p'`
|
||||
- `stringname.replace("a", "p");` will replace every `a` with `p`
|
||||
- `stringname1.equals(stringname2);` will return true of false depending on whether the two contain the same value
|
||||
- `stringname1.compareTo(stringname2);` is for alphabetical order
|
||||
- if the condition above is < 0, name1 is before name2. >0, name1 is before name2, if =0, its the same string
|
||||
- if the condition above is $`< 0`$, name1 is before name2. $`> 0`$, name1 is after name2, if $`=0`$, its the same string
|
||||
|
||||
## Sorting
|
||||
```java
|
||||
static void ImprovedBubbleSorted(int arr[]) {
|
||||
boolean isSorted = false;
|
||||
int j = 1;
|
||||
while(!isSorted) {
|
||||
isSorted = true;
|
||||
for(int i = 0; i < arr.length - j; i++) {
|
||||
if(arr[i]> arr[i + 1]) {
|
||||
int t = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = t;
|
||||
|
||||
isSorted = true;
|
||||
}
|
||||
}
|
||||
j++;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Searching
|
||||
```java
|
||||
boolean found=false;
|
||||
System.out.println("Enter the word you want to search");
|
||||
String word = sc.next();
|
||||
for(int i=0; i<arr.length; i++) {
|
||||
if(arr[i].equals(word)) {
|
||||
found=true;
|
||||
}
|
||||
}
|
||||
if(found) {
|
||||
System.out.println("The word was found");
|
||||
}
|
||||
else {
|
||||
System.out.println("The word was not found");
|
||||
}
|
||||
```
|
||||
|
||||
## Searching Continued
|
||||
```java
|
||||
boolean found=false;
|
||||
System.out.println("Enter the word you want to search");
|
||||
String word = sc.next();
|
||||
int pos=0;
|
||||
for(int i=0; i<arr.length; i++) {
|
||||
if(arr[i].equals(word)) {
|
||||
found=true;
|
||||
pos=i;
|
||||
}
|
||||
}
|
||||
if(found) {
|
||||
System.out.println("The word was found at index: " + pos);
|
||||
}
|
||||
else {
|
||||
System.out.println("The word was not found");
|
||||
}
|
||||
```
|
||||
|
||||
## Conclusion
|
||||
- Arrays are very useful when problem solving when a large numbe of data is involved
|
||||
- `Organize` large sets of data
|
||||
- Variety of ways you can access information in an array
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user