Arrays 如何查找存储并查找数组中的最高值? import java.util.Scanner; 导入java.util.array; 公共类P4{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); 最终整数毫米=12; 最终整数排序=12; int[]plantHeight=新int[排序]; int[]avgTemp={46,48,49,50,51,53,54,55,56,55,51,47}; int[]avgRain={5,3,3,1,1,0,0,0,0,1,3,4}; int[]新增长; newGrowth=newint[NUMMONTHS]; int minTemp; int-maxTemp; 因特明兰; int j; //提示用户输入 System.out.println(“欢迎来到210园艺规划师!”); System.out.println(“输入设备的最低温度:”); minTemp=input.nextInt(); System.out.println(“输入设备的最高温度:”); maxTemp=input.nextInt(); System.out.println(“输入工厂的最小降雨量:”); minRain=input.nextInt(); System.out.println(“月”+“\t”+“温雨生长”+“\t”+“株高”); //计算并打印植物生长 对于(j=0;j=minTemp&maxTemp>=avgTemp[j]){ newGrowth[j]=avgRain[j]-minRain; 如果(j==0){ 株高[j]=新生长[j]; }否则{ 株高[j]=新生长[j]+株高[j-1];} } //如果生长为零,则高度保持与上一个相同 否则{ 新增长[j]=-1; 株高[j]=株高[j]-1; } //植物高度不能小于0 if(植物高度[j]
,您可以使用inbuild函数对最后一个数字进行排序和检索 下面是一个简单的示例来解释Arrays 如何查找存储并查找数组中的最高值? import java.util.Scanner; 导入java.util.array; 公共类P4{ 公共静态void main(字符串[]args){ 扫描仪输入=新扫描仪(System.in); 最终整数毫米=12; 最终整数排序=12; int[]plantHeight=新int[排序]; int[]avgTemp={46,48,49,50,51,53,54,55,56,55,51,47}; int[]avgRain={5,3,3,1,1,0,0,0,0,1,3,4}; int[]新增长; newGrowth=newint[NUMMONTHS]; int minTemp; int-maxTemp; 因特明兰; int j; //提示用户输入 System.out.println(“欢迎来到210园艺规划师!”); System.out.println(“输入设备的最低温度:”); minTemp=input.nextInt(); System.out.println(“输入设备的最高温度:”); maxTemp=input.nextInt(); System.out.println(“输入工厂的最小降雨量:”); minRain=input.nextInt(); System.out.println(“月”+“\t”+“温雨生长”+“\t”+“株高”); //计算并打印植物生长 对于(j=0;j=minTemp&maxTemp>=avgTemp[j]){ newGrowth[j]=avgRain[j]-minRain; 如果(j==0){ 株高[j]=新生长[j]; }否则{ 株高[j]=新生长[j]+株高[j-1];} } //如果生长为零,则高度保持与上一个相同 否则{ 新增长[j]=-1; 株高[j]=株高[j]-1; } //植物高度不能小于0 if(植物高度[j],arrays,sorting,Arrays,Sorting,,您可以使用inbuild函数对最后一个数字进行排序和检索 下面是一个简单的示例来解释排序。然后可以在代码中实现 import java.util.Scanner; import java.util.Arrays; public class P4 { public static void main(String [] args) { Scanner input = new Scanner(System.in); final int NUMMONTHS = 12;
排序
。然后可以在代码中实现
import java.util.Scanner;
import java.util.Arrays;
public class P4 {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
final int NUMMONTHS = 12;
final int SORT = 12;
int [] plantHeight = new int[SORT];
int [] avgTemp = {46, 48, 49, 50, 51, 53, 54, 55, 56, 55, 51, 47};
int [] avgRain = {5, 3, 3, 1, 1, 0, 0, 0, 0, 1, 3, 4};
int[] newGrowth;
newGrowth = new int[NUMMONTHS];
int minTemp;
int maxTemp;
int minRain;
int j;
//prompt user input
System.out.println("Welcome to the 210 gardening planner!");
System.out.println("Enter minimum temperature for plant:");
minTemp = input.nextInt();
System.out.println("Enter maximum temperature for plant:");
maxTemp = input.nextInt();
System.out.println("Enter minimum rainfall for plant:");
minRain = input.nextInt();
System.out.println("Month" + "\t" + "Temp Rain Growth" + "\t" + "Plant Height");
//Calculate and print plant growth
for(j=0; j<NUMMONTHS; j++){
if(avgTemp[j] >= minTemp & maxTemp >= avgTemp[j]){
newGrowth[j] = avgRain[j] - minRain;
if(j == 0){
plantHeight[j] = newGrowth[j];
}else{
plantHeight[j] = newGrowth[j] + plantHeight[j-1];}
}
//if growth is zero, height remains same as previous
else{
newGrowth[j] = -1;
plantHeight[j] = plantHeight[j] - 1;
}
//plant height cannot be less than 0
if(plantHeight[j]< 0){
plantHeight[j] = 0;
}
plantHeight[j] = scan.nextInt();
System.out.print(j + "\t" + avgTemp[j] + " " + avgRain[j] + " ");
System.out.println(newGrowth[j] + " " + plantHeight[j]);
}
for (int i=0; i<plantHeight; i++) {
// find smallest element in elements i to plantHeight
int currentMin = plantHeight[i];
int minIndex = i;
for (int x=i+1; x < plantHeight; x++) {
if (plantHeight[x] < currentMin) {
currentMin = plantHeight[x];
minIndex = x;
}
}
if (minIndex != i) {
plantHeight[minIndex] = plantHeight[i];
plantHeight[i] = currentMin;
}
}
System.out.println(plantHeight[0]);
}
}
由于您需要最大高度及其对应的月份,因此最好使用height
作为键,将month
作为值。由于它实现了SortedMap
,因此默认情况下键将按升序插入,因此您可以检索最后一个条目,即最大高度及其对应的月份相应月份
int[] num = {4,3,6,2,9,3};
Arrays.sort(num);
for (int i: num) {
System.out.println(i);
}
int max = num[num.length - 1];
System.out.println(max); // max is 9
TreeMap map=newtreemap();
Random rand=新的Random();
对于(int i=1;i<13;i++){
map.put(兰特·耐克斯汀(100),i);
}
/*对于(Map.Entry a:Map.entrySet()){
System.out.println(a.getKey()+“”+a.getValue());
}*/
Map.Entry maxValues=Map.lastEntry();
int maxHeight=maxValues.getKey();
int maxMonth=maxValues.getValue();
System.out.println(maxHeight+“”+maxMonth);
搜索maxIndex而不是排序:
TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
Random rand = new Random();
for (int i = 1; i < 13; i++) {
map.put(rand.nextInt(100), i);
}
/*for (Map.Entry<Integer, Integer> a: map.entrySet()) {
System.out.println(a.getKey() + " " + a.getValue());
}*/
Map.Entry<Integer, Integer> maxValues = map.lastEntry();
int maxHeight = maxValues.getKey();
int maxMonth = maxValues.getValue();
System.out.println(maxHeight + " " + maxMonth);
intmaxindex=0;
int currentMax=植物高度[0];
对于(int i=1;i currentMax){
maxIndex=i;
currentMax=植物高度[i];
}
}
System.out.println(植物高度[maxIndex]);
System.out.println(avgTemp[maxIndex]);
System.out.println(avtRain[maxIndex]);
要同时查找最小值和最大值,请执行以下操作:
int maxIndex = 0;
int currentMax = plantHeight[0];
for (int i=1; i<plantHeight; i++) {
if (plantHeight[i] > currentMax) {
maxIndex = i;
currentMax = plantHeight[i];
}
}
System.out.println(plantHeight[maxIndex]);
System.out.println(avgTemp[maxIndex]);
System.out.println(avtRain[maxIndex]);
int minIndex=0;
int maxIndex=0;
int currentMin=植物高度[0];
int currentMax=currentMin;
对于(int i=1;i currentMax){
maxIndex=i;
currentMax=植物高度[i];
}
}
System.out.println(植物高度[minIndex]);
System.out.println(avgTemp[minIndex]);
System.out.println(avtRain[minIndex]);
System.out.println(植物高度[maxIndex]);
System.out.println(avgTemp[maxIndex]);
System.out.println(avtRain[maxIndex]);
谢谢!我可以在我的植物高度中找到最高的整数,但是还有一种方法可以打印最高植物高度的月份吗?@TommyTran你为什么不使用Map
而不是两个数组呢?你可以在TreeMap
上使用sort
并存储在键值中。我不知道你的意思。你能给我一个示例?@TommyTran好的。我会在几分钟后用TreeMap示例更新我的答案。关于我之前的评论,TreeMap
根据插入的key
进行排序,这样你就不必显式地对它执行排序了。这一个仍然需要遍历。如果你添加到TreeMap
,插入是,我们知道它是什么hat MAX元素将是最后一个one@sam2090-为true,但为创建映射而向TreeMap添加n个元素需要O(n log(n))。创建数组需要O(n),搜索min+max需要O(n),因此总时间仍然是O(n)(只有常量时间n的差异)。是的。我同意你的看法:)
int minIndex = 0;
int maxIndex = 0;
int currentMin = plantHeight[0];
int currentMax = currentMin;
for (int i=1; i<plantHeight; i++) {
if (plantHeight[i] < currentMin) {
minIndex = i;
currentMin = plantHeight[i];
}
else if (plantHeight[i] > currentMax) {
maxIndex = i;
currentMax = plantHeight[i];
}
}
System.out.println(plantHeight[minIndex]);
System.out.println(avgTemp[minIndex]);
System.out.println(avtRain[minIndex]);
System.out.println(plantHeight[maxIndex]);
System.out.println(avgTemp[maxIndex]);
System.out.println(avtRain[maxIndex]);