mirror of
https://github.com/Brandon-Rozek/website.git
synced 2025-10-10 15:01:15 +00:00
Website snapshot
This commit is contained in:
parent
ee0ab66d73
commit
50ec3688a5
281 changed files with 21066 additions and 0 deletions
37
content/ta/fall2017/cpsc220/oct9.md
Normal file
37
content/ta/fall2017/cpsc220/oct9.md
Normal file
|
@ -0,0 +1,37 @@
|
|||
# Lecture Notes October 9th
|
||||
|
||||
## Arrays (Cont.)
|
||||
|
||||
### Another way of Array Initialization
|
||||
|
||||
```java
|
||||
String[] names = {"Jennifer", "Noodle", "Fluffy", "Rosie", "Cinnamon", "Brianne", "Oliver"}
|
||||
```
|
||||
|
||||
Everything between the `{}` is the initial values in the names array in the order that it is written.
|
||||
|
||||
Recall that arrays are of a fixed size. The `names` array above has 7 elements.
|
||||
|
||||
### What can I do if I want to add something to the names array?
|
||||
|
||||
Do the following steps:
|
||||
|
||||
1. Create an empty array with the same size as the array
|
||||
2. Take all the contents in the array and store it in a temporary array
|
||||
3. Set names equal to another array of a bigger size
|
||||
4. Take all the contents in temp and store it back to the array of choice
|
||||
5. Add an element to the array by index
|
||||
|
||||
```java
|
||||
// (1)
|
||||
String[] temp = new String[7];
|
||||
// (2)
|
||||
temp.clone(names);
|
||||
// (3)
|
||||
names = new String[20]; // Now it can hold up to 20 names
|
||||
// (4)
|
||||
names.clone(temp);
|
||||
// (5)
|
||||
names[7] = "New name!";
|
||||
```
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue