In: Computer Science
Java programming language
Array - Single identifier which can store multiple values
Example initialized with values: int[] mySimpleArray = {4, 17, 99};
Example with fixed length but no values: int[] myFixedArray = new int[11];
The brackets identify the index. Each value has
its own index number.
NOTE: First element is the zeroth element: mySimpleArray[0] is 4,
[1] is 17
Make a new Netbeans project called ArraysAndLoops
Create the simple array with 4, 17, 99.
Use System.out.println with the variable with index literals to print out each value.
MORE LOOPS - Computing factorials
Show how factorial algorithm works, use FOR loop for factorials 0 to 10
Use i as index to store each factorial in an array
Use method Array.toString(myFactorials) to print out array at each step to check
NOTE: requires import java.util.*
WRITING A FILE - requires import java.io.*
Step 1: Make an object for file output
Step 2: Use object, method println(...)
In-Class Active: Print the factorials array to a .txt file
PrintWriter objectName = new PrintWriter(file path.filename);
Apply that structure to the array already made; use objectName factorialsTest
Make a header statement for the first line of the file:
factorialsTest.println("Factorials 0 to 10");
Make the next line the array with all its values:
factorialsTest.println(Arrays.toString(myFactorials));
These println method put the Strings in a buffer. Write to actual file by closing object.
factorialsTest.close();
If time, print out all the array values using a FOR loop, System.out
Java Code:
package arrayprog;
//Imports
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
public class ArraysAndLoops {
//Main method
public static void main(String[] args) throws
FileNotFoundException {
//Initializing an array
int arr[] = {4, 17, 99};
//Printing the array using the
index
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
//Declaring an array to store
factorials
int myFactorials[] = new
int[11];
myFactorials[0] = 1; //0! is
1
System.out.println("Factorial
values: ");
//Factorial algorithm
for(int i = 1 ; i <= 10 ; i++)
{
myFactorials[i]
= i * myFactorials[i-1]; //factorial(i) = i * factorial(i-1)
System.out.println(Arrays.toString(myFactorials)); //printing the
array at each iteration
}
//Writing to a file using a
PrintWriter
PrintWriter factorialsTest = new
PrintWriter("C:\\Users\\vkanchan\\pro\\Ques\\src\\arrayprog\\factorials.txt");
factorialsTest.println("Factorials
0 to 10");
factorialsTest.println(Arrays.toString(myFactorials));
//Closing the file
factorialsTest.close();
}
}
Sample Output:
4
17
99
Factorial values:
[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 2, 6, 0, 0, 0, 0, 0, 0, 0]
[1, 1, 2, 6, 24, 0, 0, 0, 0, 0, 0]
[1, 1, 2, 6, 24, 120, 0, 0, 0, 0, 0]
[1, 1, 2, 6, 24, 120, 720, 0, 0, 0, 0]
[1, 1, 2, 6, 24, 120, 720, 5040, 0, 0, 0]
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 0, 0]
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 0]
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]
factorials.txt:
Factorials 0 to 10
[1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]