GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ThinkJavaCode/ch08/ArrayExamples.java at master · dwynewilly/ThinkJavaCode

Expand file tree

/

ArrayExamples.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
100 lines (83 loc) · 2.31 KB
/

ArrayExamples.java

Copy path

File metadata and controls

100 lines (83 loc) · 2.31 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.Arrays;
/**
* Demonstrates uses of arrays.
*/
public class ArrayExamples {
/**
* Example code from Chapter 8.
*/
public static void main(String[] args) {
int size = 10;
int[] counts = new int[4];
double[] values = new double[size];
counts[0] = 7;
counts[1] = counts[0] * 2;
counts[2]++;
counts[3] -= 60;
// traversal with a while loop
int j = 0;
while (j < 4) {
System.out.println(counts[j]);
j++;
}
// traversal with a for loop
for (int i = 0; i < 4; i++) {
System.out.println(counts[i]);
}
int[] array = {1, 2, 3, 4};
printArray(array);
// printing an array as an object
System.out.println(array);
// printing with Arrays class
System.out.println(Arrays.toString(array));
// copying an array
double[] a = {1.0, 2.0, 3.0};
double[] b = new double[a.length];
for (int i = 0; i < a.length; i++) {
b[i] = a[i];
}
// copying with Arrays class
double[] c = Arrays.copyOf(a, a.length);
// traversal
for (int i = 0; i < a.length; i++) {
a[i] = Math.pow(a[i], 2.0);
}
// search
int index = search(a, 2.0);
System.out.println("index = " + index);
// reduce
double total = sum(a);
System.out.println("total = " + total);
}
/**
* Prints the elements of an array.
*/
public static void printArray(int[] array) {
System.out.print("{" + array[0]);
for (int i = 1; i < array.length; i++) {
System.out.print(", " + array[i]);
}
System.out.println("}");
}
/**
* Returns the index of the target in the array, or -1 if not found.
*/
public static int search(double[] a, double target) {
for (int i = 0; i < a.length; i++) {
if (a[i] == target) {
return i;
}
}
return -1;
}
/**
* Returns the total of the elements in an array.
*/
public static double sum(double[] a) {
double total = 0.0;
for (int i = 0; i < a.length; i++) {
total += a[i];
}
return total;
}
}
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.