-
Notifications
You must be signed in to change notification settings - Fork 16
/
Lesson_02_1D_Array_Toy_Examples.java
123 lines (107 loc) · 4.85 KB
/
Lesson_02_1D_Array_Toy_Examples.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Hello World with Dr. Dan - A Complete Introduction to Programming from Java to C++ (Code and Course © Dan Grissom)
//
// Additional Lesson Resources from Dr. Dan:
// High-Quality Video Tutorials: www.helloDrDan.com
// Free Commented Code: https://github.com/DanGrissom/hello-world-dr-dan-java
//
// In this lesson you will learn:
// 1) 1D Arrays
// a) Declaring 1D arrays with constant data
// b) Declaring 1D arrays from a variable
// c) Iterating through and printing 1D arrays
// 2) Random class
// a) Generating random numbers
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import java.util.Random;
import java.util.Scanner;
public class Lesson_02_1D_Array_Toy_Examples {
///////////////////////////////////////////////////////////////
// MAIN - Entry point where code will begin execution for file
///////////////////////////////////////////////////////////////
public static void main(String[] args) {
// Simple welcome statements printed to screen
System.out.println("Program Objective: Learn to store and access data in 1D arrays using a toy example");
System.out.println("===========================================================================");
// Initialize Scanner to read in from user
Scanner scan = new Scanner(System.in);
// Initialize random number generator
Random randy = new Random();
//////////////////////////////////////////////////////////////////
// Hard-Coded Arrays
// Create a hard-coded 1D array
int [] arrHardCodedNums = { 1, 5, 10, 5, 1, 20, 100 };
arrHardCodedNums[0] = 1000;
//System.out.println( Arrays.toString(arrHardCodedNums) );
printArray("Hard-Coded 1D Array (of numbers)", arrHardCodedNums);
int [] arrHardCodedNums2 = { 4, 5, 6, 7, 8 };
printArray("Second Hard-Coded 1D Array (of numbers)", arrHardCodedNums2);
//String [] arrHardCodedWords = { "Monday", "Tuesday", "Wednesday" };
//System.out.println( Arrays.toString(arrHardCodedWords) );
//////////////////////////////////////////////////////////////////
// Variable-Sized Arrays
// Prompt user for a number of elements to store
System.out.print("\n\nEnter a number of random numbers to generate: ");
int numElements = scan.nextInt();
// Create 1D array from initial size
int [] a = new int [numElements];
for (int i = 0; i < a.length; i++)
a[i] = randy.nextInt(200);
printArray("Randomly Generated 1D Array", a);
//////////////////////////////////////////////////////////////////
// Array Modification & Search
// Prompt user to change the value at a particular index
System.out.print("\n\nEnter an index you'd like to overwrite: ");
int iNew = scan.nextInt();
System.out.printf("Enter a value to overwrite the current data (a[%s] = %s): ", iNew, a[iNew]);
int vNew = scan.nextInt();
// Overwrite value and print updated array
a[iNew] = vNew;
printArray("Updated Randomly Generated 1D Array", a);
// Prompt user to overwrite a particular value, regardless of its index (Same as search)
System.out.print("\n\nEnter a value to overwrite the current data (index unknown): ");
int findVal = scan.nextInt();
// Search for value and replace if found
boolean foundVal = false;
for (int i = 0; i < a.length; i++) {
if (a[i] == findVal) {
System.out.printf("FOUND - Enter a value to overwrite the current data (a[%s] = %s): ", i, a[i]);
vNew = scan.nextInt();
a[i] = vNew;
foundVal = true;
//break; // Only replace the first value
}
}
// Print result of the search
if (foundVal)
printArray("Updated Randomly Generated 1D Array", a);
else
System.out.printf("\t%s not found in the array; array unchanged", findVal);
}
///////////////////////////////////////////////////////////////////////////////////////
// This prints the array passed in via parameter. In addition, it shows the array
// indices and array "name" (also passed in as a parameter).
// Parameters:
// arrayName - String representing name of array to print
// array - array of ints to print
//
// Returns:
// void (nothing)
///////////////////////////////////////////////////////////////////////////////////////
private static void printArray(String arrayName, int [] array) {
// Print header
System.out.printf("\n\nPrinting %s (size %s)\n", arrayName, array.length);
System.out.println("=================================");
// Print index numbers
for (int i = 0; i < array.length; i++)
System.out.printf("a[%s]\t", i);
// Print = sign for each index/value
System.out.println();
for (int i = 0; i < array.length; i++)
System.out.printf("=\t", i);
// Print array values
System.out.println();
for (int i = 0; i < array.length; i++)
System.out.printf("%s\t", array[i]);
}
}