1. How to iterate over an array:
public static void Printfarray (int[] arr)
2. Get the maximum value in the array:
public static int Getmax (int[] arr)
public class Arraytool {
By making the construction method private, the outside world cannot create objects.
/**
* This is a private structure
*/
Private Arraytool () {}
/**
* This is the way to iterate through the array, the following format is: [element 1, Element 2, Element 3, ...]
* @param arr This is the array to be traversed
*/
public static void PrintArray (int[] arr) {
System.out.print ("[");
for (int x=0; x<arr.length; x + +) {
if (x = = arr.length-1) {
System.out.println (arr[x]+ "]");
}else {
System.out.print (arr[x]+ ",");
}
}
}
/**
* This is the method to get the maximum value in the array
* @param arr This is an array to get the maximum value
* @return Returns the maximum value in the array
*/
public static int Getmax (int[] arr) {
int max = arr[0];
for (int x=1; x<arr.length; x + +) {
if (Arr[x] > max) {
max = arr[x];
}
}
return Max;
}
/**
* Gets the index of the first occurrence of the specified element in the array, and returns 1 if the element does not exist
* @param arr Array to be looked up
* @param value to find the element
* @return Returns the index of the element in the array, or 1 if it does not exist
*/
public static int GetIndex (int[] arr,int value) {
int index =-1;
for (int x=0; x<arr.length; x + +) {
if (arr[x] = = value) {
index = x;
Break
}
}
return index;
}
}
Tool classes for arrays in Java