我需要打印数组的最大元素,但要显示其索引号
I need to print the largest element of an Array, but showing its index number
我有一个 50.I 的固定变量,有两个不同的数组(固定数字)。我需要将第一个变量 (50) 与第一个数组的元素相加,然后将结果除以每个元素另一个数组。然后我需要打印该数组的最大数量,但显示索引号(应为 2)。想不通如何通过引用最大数来拉取索引
我的代码使用硬编码显示索引,我需要解决这个问题,你能帮我吗?
public static void main(String[] args) {
int h = 50;
int d [] = { 10, 25, 5 };
int z [] = { 2, 3, 1 };
String result = "";
for (int i = 0; i < d.length; i++) {
result += ((h + d[i]) / z[i]) + ",";
}
String str = result;
String[] string = str.replaceAll("\[", "").replaceAll("]", "").split(",");
int[] arr = new int[string.length];
for (int i = 0; i < string.length; i++) {
arr[i] = Integer.valueOf(string[i]);
}
// CAN'T USE HARD CODE HERE, PLEASE HELP
Arrays.stream(arr).max().getAsInt();
System.out.println("The largest number index is: " + findIndex(arr, 55));
}
public static int findIndex(int arr[], int t) {
ArrayList<Integer> list = new ArrayList<>();
for (int i : arr)
list.add(i);
return list.indexOf(t);
}
}
我不明白为什么要将结果存储为字符串,然后将此字符串转换为 int 数组。可以直接保存int数组。
public static void main(String[] args) {
int h = 50;
int d [] = { 10, 25, 5 };
int z [] = { 2, 3, 1 };
int result [] = new int[d.length];
int max = -1;
int indexMax = -1;
for (int i = 0; i < d.length; i++) {
result[i] = ((h + d[i]) / z[i]);
if(result[i]>max){
max = result[i];
indexMax = i;
}
}
System.out.println(max);
System.out.println(indexMax);
}
这是我对你的问题的解决方案
这个答案是通用的(您可以更改数组的值,但它必须在索引中等于)
import java.util.Arrays;
import java.util.Collections;
public class Main{
public static void main(String[] args) {
int h = 50;
int d [] = { 10, 25, 5 };
int z [] = { 2, 3, 1 };
// here we sum every element of the first array to variable h (that equals 50)
for (int i = 0 ; i < d.length ; i++){
d [i] = d[i] + h ;
}
// here we divide every element of the first array to other array .
for (int i = 0 ; i < d.length ; i++){
d [i] = d[i] / z[i] ;
}
// here we want to find the max
int max = Arrays.stream(d).max().getAsInt();
// here we get the index of the max value
int index = 0;
for (int i = 0 ; i < d.length ; i++){
if (d[i] == max){
index = i;
break;
}
}
System.out.println("The largest number index is: " + index);
}}
我有一个 50.I 的固定变量,有两个不同的数组(固定数字)。我需要将第一个变量 (50) 与第一个数组的元素相加,然后将结果除以每个元素另一个数组。然后我需要打印该数组的最大数量,但显示索引号(应为 2)。想不通如何通过引用最大数来拉取索引
我的代码使用硬编码显示索引,我需要解决这个问题,你能帮我吗?
public static void main(String[] args) {
int h = 50;
int d [] = { 10, 25, 5 };
int z [] = { 2, 3, 1 };
String result = "";
for (int i = 0; i < d.length; i++) {
result += ((h + d[i]) / z[i]) + ",";
}
String str = result;
String[] string = str.replaceAll("\[", "").replaceAll("]", "").split(",");
int[] arr = new int[string.length];
for (int i = 0; i < string.length; i++) {
arr[i] = Integer.valueOf(string[i]);
}
// CAN'T USE HARD CODE HERE, PLEASE HELP
Arrays.stream(arr).max().getAsInt();
System.out.println("The largest number index is: " + findIndex(arr, 55));
}
public static int findIndex(int arr[], int t) {
ArrayList<Integer> list = new ArrayList<>();
for (int i : arr)
list.add(i);
return list.indexOf(t);
}
}
我不明白为什么要将结果存储为字符串,然后将此字符串转换为 int 数组。可以直接保存int数组。
public static void main(String[] args) {
int h = 50;
int d [] = { 10, 25, 5 };
int z [] = { 2, 3, 1 };
int result [] = new int[d.length];
int max = -1;
int indexMax = -1;
for (int i = 0; i < d.length; i++) {
result[i] = ((h + d[i]) / z[i]);
if(result[i]>max){
max = result[i];
indexMax = i;
}
}
System.out.println(max);
System.out.println(indexMax);
}
这是我对你的问题的解决方案
这个答案是通用的(您可以更改数组的值,但它必须在索引中等于)
import java.util.Arrays;
import java.util.Collections;
public class Main{
public static void main(String[] args) {
int h = 50;
int d [] = { 10, 25, 5 };
int z [] = { 2, 3, 1 };
// here we sum every element of the first array to variable h (that equals 50)
for (int i = 0 ; i < d.length ; i++){
d [i] = d[i] + h ;
}
// here we divide every element of the first array to other array .
for (int i = 0 ; i < d.length ; i++){
d [i] = d[i] / z[i] ;
}
// here we want to find the max
int max = Arrays.stream(d).max().getAsInt();
// here we get the index of the max value
int index = 0;
for (int i = 0 ; i < d.length ; i++){
if (d[i] == max){
index = i;
break;
}
}
System.out.println("The largest number index is: " + index);
}}