2.1.16 Write a static method scale() that takes a double array as its argument and has the side effect of scaling the array so that each element is between 0 and 1 (by subtracting the minimum value from each element and then dividing each element by the difference between the minimum and maximum values). Use the max() method defined in the table in the text, and write and use a matching min() method.
public class Work{
public static double max(double[] a){
double max = Double.NEGATIVE_INFINITY;
for(int i = 0; i<a.length; i++){
if(a[i]>max){
max=a[i];
}
}
return max;
}
public static double min(double[] a){
double min = Double.POSITIVE_INFINITY;
for(int i = 0; i<a.length; i++){
if(a[i]<min){
min=a[i];
}
}
return min;
}
public static double[] scale(double[] a){
double[] b = new double[a.length];
double low = min(a);
double high = max(a);
for(int i = 0; i<a.length; i++){
b[i]=(a[i]-low)/(high-low);
}
return b;
}
public static double[] randomArray(int n){
double[] a = new double[n];
for(int i = 0; i<n; i++){
a[i]=Math.random()*10;
}
return a;
}
public static void main(String[] args){
int n = Integer.parseInt(args[0]);
double[] a = randomArray(n);
double[] b = scale(a);
for(int i = 0; i<n; i++){
System.out.print(b[i]+" ");
}
}
}
2.1.17 Write a static method reverse() that takes an array of strings as its argument and returns a new array with the strings in reverse order. (Do not change the order of the strings in the argument array.) Write a static method reverseInplace() that takes an array of strings as its argument and produces the side effect of reversing the order of the strings in the argument array.
public class Work {
/* 反转数组*/
static void reverse(int a[], int n)
{
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
/*输入反转数组*/
System.out.println("reversed array:");
for (int k = 0; k < n; k++) {
System.out.print(b[k]+" ");
}
}
static void reverseInplace(int a[], int n)
{
int i, k, t;
for (i = 0; i < n / 2; i++) {
t = a[i];
a[i] = a[n - i - 1];
a[n - i - 1] = t;
}
System.out.println("reversed array:");
for (k = 0; k < n; k++) {
System.out.print(a[k]+" ");
}
}
public static void main(String[] args)
{
int [] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
System.out.println("\n reversing the order of the strings in the argument array:");
reverseInplace(arr, arr.length);
}
}
Java 实例 – 数组反转 | 菜鸟教程 (runoob.com)
2.1.19 Write a static method histogram() that takes an int array a[] and an integer m as arguments and returns an array of length m whose ith element is the number of times the integer i appeared in a[]. Assuming the values in a[] are all between 0 and m-1, the sum of the values in the returned array should equal a.length.
public class Work{
public static int[] histogram(int[] a,int m){
int[] b = new int[m];
int n = a.length;
for(int i = 0; i<n; i++){
for(int j = 0; j<m; j++){
if(a[i]==j){
b[j]++;
}
}
}
return b;
}
public static void main(String[] args){
int m = Integer.parseInt(args[0]);
int[] a = {1,2,2,3,3};
int[] c = histogram(a, m);
int n = a.length;
for(int i = 0; i<n; i++){
System.out.print(a[i]+" ");}
System.out.print("\n");
for(int j = 0; j<m; j++){
System.out.print(c[j]+" ");
}
}
}