Last active
December 27, 2021 00:16
-
-
Save Kev-in123/dfc3b1b26a4570663c07253077d47199 to your computer and use it in GitHub Desktop.
Test: Arrays & File I/O
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayExamples { | |
public static void main(String[] args) { | |
// create an array of integers of length 10 | |
int[] arr = new int[10]; | |
// store the size of the array in a variable | |
int len = arr.length; | |
// use a for-each loop to | |
for (int i: arr) { | |
System.out.println(i); | |
// the output will be 10 lines of 0s since we haven't modified it | |
// when an array in java is declared | |
// numbers: all 0s | |
// strings: all null strings | |
// booleans: all false | |
} | |
System.out.println("-------------------"); | |
for (int i = len; i > 0; i--) { | |
// print the array in reverse | |
// we do `i-1` because array indexes start at 0 and end at 1 less than the length | |
// since we are starting from the length, we must go back one index | |
// and since we used `i > 0` and not `i >= 0`, the last iteration of the loop will make i equal to 1 | |
// so we subtract 1 to make i = 0 when used so we can access the first element in the array | |
System.out.println(arr[i-1]); | |
// the output will still be 10 lines of 0s since we haven't modified it | |
} | |
System.out.println("-------------------"); | |
int[] arr1 = {0, 1, 2, 3, 4}; | |
int[] arr2 = {0, 1, 2, 3, 4}; | |
boolean equals = true; | |
int length1 = arr1.length; | |
int length2 = arr2.length; | |
for (int i = 0; i < length1; i++) { | |
for (int j = 0; j < length2; j++) { | |
if (arr1[i] != arr2[j]) { | |
System.out.println("Not equals"); | |
equals = false; | |
break; | |
} | |
// increment i here to move the pointer back | |
i++; | |
} | |
if (!equals) { | |
break; | |
} | |
} | |
if (equals) { | |
System.out.println("Equals"); | |
} | |
// Output: Equals | |
System.out.println("-------------------"); | |
int[] arr1 = {0, 1, 2, 3, 4}; | |
int[] arr2 = {0, 1, 2, 3, 5}; | |
boolean equals = true; | |
int length1 = arr1.length; | |
int length2 = arr2.length; | |
for (int i = 0; i < length1; i++) { | |
for (int j = 0; j < length2; j++) { | |
if (arr1[i] != arr2[j]) { | |
System.out.println("Not equals"); | |
equals = false; | |
break; | |
} | |
// increment i here to move the pointer back | |
i++; | |
} | |
if (!equals) { | |
break; | |
} | |
} | |
if (equals) { | |
System.out.println("Equals"); | |
} | |
// Output: Not equals | |
System.out.println("-------------------"); | |
int[] array1 = {0, 1, 2, 3, 4}; | |
int[] array2 = {0, 1, 2, 3, 4}; | |
int[] array3 = addArr(array1, array2); | |
for(int i: array3) { | |
System.out.println(i); | |
} | |
} | |
public static int[] addArr (int[] arr1, int[] arr2) { | |
int length1 = arr1.length; | |
int length2 = arr2.length; | |
int[] arr3 = new int[length1 + length2]; | |
for (int i = 0; i < length1; i++) { | |
arr3[i] = arr1[i]; | |
} | |
for (int i = 0; i < length2; i++) { | |
arr3[length1 + i] = arr2[i]; | |
} | |
return arr3; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class File_IO_read { | |
public static void main(String[] args) { | |
// file name | |
String fileName = "read_demo.txt"; | |
// declare line as a string | |
String line; | |
try { | |
// creates a file reader and buffer | |
FileReader file = new FileReader(fileName); | |
BufferedReader in = new BufferedReader(file); | |
// reads the first line | |
line = in.readLine(); | |
// checks if the line has content | |
while (line != null) { | |
// prints the content of the line | |
System.out.println(line); | |
// continues with the next line | |
line = in.readLine(); | |
} | |
// close the file | |
in.close(); | |
} catch (IOException e) { | |
// in case there was a problem reading the file | |
System.out.println("Error"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class File_IO_write { | |
public static void main(String[] args) { | |
String fileName = "write_demo.txt"; | |
try { | |
FileWriter file = new FileWriter(fileName); // Add a true paramater to continue writing to to file rather than overwriting it | |
// FileWriter file = new FileWriter(fileName, true); | |
BufferedWriter out = new BufferedWriter(file); | |
out.write("Hello, World!"); | |
out.write(">"); | |
out.newLine(); | |
out.write(":D"); | |
out.close(); | |
} catch (IOException e) { | |
System.out.println("Error"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Arrays: | |
There aren't that many built-ins for arrays (importing `java.util.Arrays` doesn't count) | |
- array.length returns the size of the array | |
- to compare 2 arrays, you can use Arrays.deepEquals (you would need to import java.util.Arrays) or you can iterate over each array and compare the values | |
- to add arrays, you can create a new array, and iterate over the original arrays and add them to the new array | |
File I/O: | |
To create a file input stream: | |
FileReader file = new FileReader(fileName); | |
To create a file buffer reader: | |
BufferedReader in = new BufferedReader(file); | |
To create a file output stream: | |
FileWriter file = new FileWriter(fileName); // Add a true paramater to continue writing to to file rather than overwriting it | |
To create a file buffer writer: | |
BufferedWriter out = new BufferedWriter(file); | |
Note that you need to import `java.io.*` | |
Since there is a coding part: | |
Naming conventions: | |
1) under 256 characters | |
2) only letters, digits and underscores (_) | |
3) can’t start with a digit | |
4) no spaces | |
5) no reserved keywords (refer to quiz 1 study notes for a list of reserved keywords) | |
Data types: | |
boolean - stores true or false | |
byte - stores whole numbers from -128 to 127 | |
char - stores a single character/ASCII values | |
int - stores whole numbers from -2,147,483,648 to 2,147,483,647 | |
long - stores whole numbers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | |
short - stores whole numbers from -32,768 to 32,767 | |
float - stores fractional numbers, sufficient for storing 6 to 7 decimal digits | |
double - stores fractional numbers, sufficient for storing 15 decimal digits | |
Assignment Operators: | |
a = b; | |
- assigns b to a | |
a += b; - same as a = a + b; | |
- can be used to increment a number by b | |
- or to concatenate strings | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
a -= b; - same as a = a - b; | |
- can be used to decrement a number by b | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
a *= b; - same as a = a * b | |
- can be used to multiply a number by b | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
a /= b; - same as a = a / b | |
- can be used to divide a number by b | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
a %= b; - same as a = a % b | |
- can be used to divide a number by b and return the remainder | |
- note that if you do this to 2 chars, the chars will be converted to ints (their ascii value) | |
Arithmetic Operators: | |
+ adds the numbers (also used to concatenate strings to strings/numbers/chars) | |
- subtracts the numbers | |
* multiplies the numbers | |
/ divides the numbers (if both numbers used are both integers, drop the decimal once done dividing) | |
% divides the numbers and returns the remainder | |
++ increments the number by 1 | |
-- decrements the number by 1 | |
Logical Operators: | |
|| - or | |
&& - and | |
! - not | |
Comparison Operators: | |
== - equals to (does not apply to strings; however, you can use these to compare chars) | |
!= - not equals to (does not apply to strings; however, you can use these to compare chars) | |
> - greater than (does not apply to strings; however, you can use these to compare chars) | |
< - less than (does not apply to strings; however, you can use these to compare chars) | |
<= - less than or equal to (note the order of the angle bracket and the equals sign) (does not apply to strings; however, you can use these to compare chars) | |
>= - greater than or equal to (note the order of the angle bracket and the equals sign) (does not apply to strings; however, you can use these to compare chars) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello there | |
Second line | |
Third line |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello, World!> | |
:D |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment