Java 17 Recipes


-4. Working with Fix-Sized Arrays


Download 3.2 Mb.
Pdf ko'rish
bet135/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   131   132   133   134   135   136   137   138   ...   245
Bog'liq
Java 17 Recipes

7-4. Working with Fix-Sized Arrays
 Problem
You need a simple data structure that can store a fixed (and possibly large) amount of 
same-typed data and provide fast sequential access.
 Solution
Consider using an array. While Java provides more sophisticated and flexible Collection 
types, the array type can be a useful data structure for many applications. The following 
example demonstrates the simplicity of working with arrays. The GradeAnalyzer class 
provides a means for calculating various grade-related statistics, such as the mean 
(average) grade, minimum grade, and maximum grade.
public class GradeAnalyzer {
// The internal grades array
private int[] grades;
public void setGrades(int[] grades) {
this.grades = grades;
}
// Return cloned grades so the caller cannot modify our internal grades
public int[] getGrades() {
return grades != null ? grades.clone() : null;
}
Chapter 7 Data SourCeS anD ColleCtionS


255
public int meanGrade() {
int mean = 0;
if (grades != null&& grades.length > 0) {
int sum = 0;
for (int i = 0; i < grades.length; i++) {
sum += grades[i];
}
mean = sum / grades.length;
}
return mean;
}
public int minGrade() {
int min = 0;
for (int index = 0; index < grades.length; index++) {
if (grades[index] < min) {
min = grades[index];
}
}
return min;
}
public int maxGrade() {
int max = 0;
for (int index = 0; index < grades.length; index++) {
if (grades[index] > max) {
max = grades[index];
}
}
return max;
}
static int[] initGrades1() {
int[] grades = new int[5];
grades[0] = 77;
grades[1] = 48;
grades[2] = 69;
grades[3] = 92;
Chapter 7 Data SourCeS anD ColleCtionS


256
grades[4] = 87;
return grades;
}
static int[] initGrades2() {
int[] grades = { 57, 88, 67, 95, 99, 74, 81 };
return grades;
}
static int[] initGrades3() {
return new int[]{ 100, 70, 55, 89, 97, 98, 82 };
}
public static void main(String... args) {
GradeAnalyzer ga = new GradeAnalyzer();
ga.setGrades(initGrades1());
System.out.println("Grades 1:");
System.out.println("Mean of all grades is " + ga.meanGrade());
System.out.println("Min grade is " + ga.minGrade());
System.out.println("Max grade is " + ga.maxGrade());
ga.setGrades(initGrades2());
System.out.println("Grades 2:");
System.out.println("Mean of all grades is " + ga.meanGrade());
System.out.println("Min grade is " + ga.minGrade());
System.out.println("Max grade is " + ga.maxGrade());
ga.setGrades(initGrades3());
System.out.println("Grades 3:");
System.out.println("Mean of all grades is " + ga.meanGrade());
System.out.println("Min grade is " + ga.minGrade());
System.out.println("Max grade is " + ga.maxGrade());
Object testArray = ga.getGrades();
Class testClass = testArray.getClass();
System.out.println("isArray: " + testClass.isArray());
System.out.println("getClass: " + testClass.getName());
System.out.println("getSuperclass: " +
testClass.getSuperclass().getName());
Chapter 7 Data SourCeS anD ColleCtionS


257
System.out.println("getComponentType: " +
testClass.getComponentType());
System.out.println("Arrays.toString: " +
Arrays.toString((int[])testArray));
}
}
Running this code result in the following output.
Grades 1:
Mean of all grades is 74
Min grade is 0
Max grade is 92
Grades 2:
Mean of all grades is 80
Min grade is 0
Max grade is 99
Grades 3:
Mean of all grades is 84
Min grade is 0
Max grade is 100
isArray: true
getClass: [I
getSuperclass: class java.lang.Object
getComponentType: int
Arrays.toString: [100, 70, 55, 89, 97, 98, 82]

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   131   132   133   134   135   136   137   138   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling