001package arcommon; 002 003import java.util.Arrays; 004 005public class ArrayUtil { 006 public static int[] sortedArray(int[] array) { 007 int[] result = Arrays.copyOf(array, array.length); 008 Arrays.sort(result); 009 return result; 010 } 011 012 public static int min(int[] array) { 013 if (array.length == 0) { 014 throw new RuntimeException("array.length == 0"); 015 } 016 int result = array[0]; 017 for (int i = 1; i < array.length; i++) { 018 result = Math.min(result, array[i]); 019 } 020 return result; 021 } 022 023 public static double max(double[][] array) { 024 if (array.length == 0) { 025 throw new RuntimeException("array.length == 0"); 026 } 027 double result = max(array[0]); 028 for (int i = 1; i < array.length; i++) { 029 double maximum = max(array[i]); 030 if (result < maximum) { 031 result = maximum; 032 } 033 } 034 return result; 035 } 036 037 public static double max(double[] array) { 038 if (array.length == 0) { 039 throw new RuntimeException("array.length == 0"); 040 } 041 double result = array[0]; 042 for (int i = 1; i < array.length; i++) { 043 if (result < array[i]) { 044 result = array[i]; 045 } 046 } 047 return result; 048 } 049 050 public static int max(int[] array) { 051 if (array.length == 0) { 052 throw new RuntimeException("array.length == 0"); 053 } 054 int result = array[0]; 055 for (int i = 1; i < array.length; i++) { 056 result = Math.max(result, array[i]); 057 } 058 return result; 059 } 060 061 public static boolean contains(int[] array, int value) { 062 for (int i = 0; i < array.length; i++) { 063 if (array[i] == value) { 064 return true; 065 } 066 } 067 return false; 068 } 069 070 public static int indexOf(String[] array, String value) { 071 for (int i = 0; i < array.length; i++) { 072 if (array[i].equals(value)) { 073 return i; 074 } 075 } 076 return -1; 077 } 078 079 public static int indexOf(Object[] array, Object value) { 080 for (int i = 0; i < array.length; i++) { 081 if (array[i].equals(value)) { 082 return i; 083 } 084 } 085 return -1; 086 } 087 088 public static int indexOf(int[] array, int value) { 089 for (int i = 0; i < array.length; i++) { 090 if (array[i] == value) { 091 return i; 092 } 093 } 094 return -1; 095 } 096 097 public static int indexOf(char[] array, char value) { 098 for (int i = 0; i < array.length; i++) { 099 if (array[i] == value) { 100 return i; 101 } 102 } 103 return -1; 104 } 105 106 public static int indexOf(double[] array, double value) { 107 for (int i = 0; i < array.length; i++) { 108 if (array[i] == value) { 109 return i; 110 } 111 } 112 return -1; 113 } 114 115 public static int sumArray(int[] array) { 116 int result = 0; 117 for (int i = 0; i < array.length; i++) { 118 result += array[i]; 119 } 120 return result; 121 } 122 123 public static long sumArray(long[] array) { 124 long result = 0; 125 for (int i = 0; i < array.length; i++) { 126 result += array[i]; 127 } 128 return result; 129 } 130 131 public static int[] reverse(int[] array) { 132 int[] result = new int[array.length]; 133 for (int i = 0; i < array.length; i++) { 134 result[array.length - i - 1] = array[i]; 135 } 136 return result; 137 } 138}