Java-在2D数组中查找最大数
此程序比较用户输入的2*5数组中的两个数字,并显示两者之间的最大数字。但是我在理解Java-在2D数组中查找最大数,java,arrays,loops,Java,Arrays,Loops,此程序比较用户输入的2*5数组中的两个数字,并显示两者之间的最大数字。但是我在理解[0]在int max=array[I][0]中真正做了什么时遇到了一些问题。我的意思是,它不应该是intmax=array[I][j]?因为基本上,我从array[I][0]中了解到,为行输入的数字将与列的第一个元素进行比较,因为它是[0],但我想解释一下它的实际工作原理 import java.util.*; public class L2 { public static void main(Str
[0]
在int max=array[I][0]
中真正做了什么时遇到了一些问题。我的意思是,它不应该是intmax=array[I][j]
?因为基本上,我从array[I][0]
中了解到,为行输入的数字将与列的第一个元素进行比较,因为它是[0]
,但我想解释一下它的实际工作原理
import java.util.*;
public class L2 {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("Please enter 10 integers");
int array[][]= new int[2][5];
for(int i=0; i<array.length; i++){
int max= array[i][0];
for(int j=0; j<array[i].length; j++){
array[i][j]= input.nextInt();
if(max<array[i][j]){
max= array[i][j];
}
}
System.out.println("Maximum number in the row is "+ max);
System.out.println();
}
}
}
import java.util.*;
公共级L2{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
System.out.println(“请输入10个整数”);
int数组[][]=新的int[2][5];
对于(int i=0;i它取一个2*5的整数数组,并在每行5中找到最大值,(然后大概将这些(2)值相加,尽管您的snipper中不包含该代码)
线路
int max= array[i][0];
正在将第i
行上的最大值初始化为该行中的第一个值,而不是扫描该行以查找更大的值并相应地增加max
。通常,您会从1开始循环计数器,因为第0个值已被处理:
for(int j=1; j<array[i].length; j++){
for(int j=1;j它取一个2*5的整数数组,并在每行5中找到最大值,(然后大概将这些(2)值求和,尽管您的snipper中不包含该代码)
线路
int max= array[i][0];
正在将第i
行上的最大值初始化为该行中的第一个值,而不是扫描该行以查找更大的值并相应地增加max
。通常,您会从1开始循环计数器,因为第0个值已被处理:
for(int j=1; j<array[i].length; j++){
for(int j=1;j它取一个2*5的整数数组,并在每行5中找到最大值,(然后大概将这些(2)值求和,尽管您的snipper中不包含该代码)
线路
int max= array[i][0];
正在将第i
行上的最大值初始化为该行中的第一个值,而不是扫描该行以查找更大的值并相应地增加max
。通常,您会从1开始循环计数器,因为第0个值已被处理:
for(int j=1; j<array[i].length; j++){
for(int j=1;j它取一个2*5的整数数组,并在每行5中找到最大值,(然后大概将这些(2)值求和,尽管您的snipper中不包含该代码)
线路
int max= array[i][0];
正在将第i
行上的最大值初始化为该行中的第一个值,而不是扫描该行以查找更大的值并相应地增加max
。通常,您会从1开始循环计数器,因为第0个值已被处理:
for(int j=1; j<array[i].length; j++){
for(int j=1;j在下一行中,max用行的第一个元素初始化:-
int max= array[i][0];
您的目的是找到每一行的最大值,因此基本上您是从该行的第一个元素开始每次迭代。在下一行中,最大值是由该行的第一个元素初始化的:-
int max= array[i][0];
//I hope this helps you
public class Compare{
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int row =2;
int col = 5;
int array[][]= new int[row][col];
System.out.println("Please enter 10 integers");
for(int i=0; i<row; i++){
for(int j=0; j<col;j++){
array[i][j]= input.nextInt();
}
}
int max = Integer.MIN_VALUE;
for(int i=0; i<row; i++){
for(int j =0; j<col; j++){
if(max<array[i][j]){
max = array[i][j];
}
}
System.out.println("Maximum number in the row is "+ max);
System.out.println();
}
}
}
您的目的是找到每一行的最大值,因此基本上您是从该行的第一个元素开始每次迭代。在下一行中,最大值是由该行的第一个元素初始化的:-
int max= array[i][0];
//I hope this helps you
public class Compare{
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int row =2;
int col = 5;
int array[][]= new int[row][col];
System.out.println("Please enter 10 integers");
for(int i=0; i<row; i++){
for(int j=0; j<col;j++){
array[i][j]= input.nextInt();
}
}
int max = Integer.MIN_VALUE;
for(int i=0; i<row; i++){
for(int j =0; j<col; j++){
if(max<array[i][j]){
max = array[i][j];
}
}
System.out.println("Maximum number in the row is "+ max);
System.out.println();
}
}
}
您的目的是找到每一行的最大值,因此基本上您是从该行的第一个元素开始每次迭代。在下一行中,最大值是由该行的第一个元素初始化的:-
int max= array[i][0];
//I hope this helps you
public class Compare{
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int row =2;
int col = 5;
int array[][]= new int[row][col];
System.out.println("Please enter 10 integers");
for(int i=0; i<row; i++){
for(int j=0; j<col;j++){
array[i][j]= input.nextInt();
}
}
int max = Integer.MIN_VALUE;
for(int i=0; i<row; i++){
for(int j =0; j<col; j++){
if(max<array[i][j]){
max = array[i][j];
}
}
System.out.println("Maximum number in the row is "+ max);
System.out.println();
}
}
}
您的目的是找到每行的最大值,因此基本上您是从该行的第一个元素开始每次迭代。//我希望这对您有所帮助
//I hope this helps you
public class Compare{
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
int row =2;
int col = 5;
int array[][]= new int[row][col];
System.out.println("Please enter 10 integers");
for(int i=0; i<row; i++){
for(int j=0; j<col;j++){
array[i][j]= input.nextInt();
}
}
int max = Integer.MIN_VALUE;
for(int i=0; i<row; i++){
for(int j =0; j<col; j++){
if(max<array[i][j]){
max = array[i][j];
}
}
System.out.println("Maximum number in the row is "+ max);
System.out.println();
}
}
}
公共类比较{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int行=2;
int col=5;
int数组[][]=新的int[行][col];
System.out.println(“请输入10个整数”);
for(inti=0;i//我希望这对您有所帮助
公共类比较{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int行=2;
int col=5;
int数组[][]=新的int[行][col];
System.out.println(“请输入10个整数”);
for(inti=0;i//我希望这对您有所帮助
公共类比较{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int行=2;
int col=5;
int数组[][]=新的int[行][col];
System.out.println(“请输入10个整数”);
for(inti=0;i//我希望这对您有所帮助
公共类比较{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int行=2;
int col=5;
int数组[][]=新的int[行][col];
System.out.println(“请输入10个整数”);
对于(inti=0;i,您可以用Collection.max(Stream.of(..).collect(Collectors.toList());
这里有一个例子
System.out.println(Collections.max(Stream.of(new int[][]{{1, 2}, {3, 4}})
.map(a -> Collections.max(Arrays.stream(a).boxed()
.collect(Collectors.toList())))
.collect(Collectors.toList())));
Output: 4
换句话说,您可以使用Collection.max(Stream.of(..).collect(Collectors.toList());
这里有一个例子
System.out.println(Collections.max(Stream.of(new int[][]{{1, 2}, {3, 4}})
.map(a -> Collections.max(Arrays.stream(a).boxed()
.collect(Collectors.toList())))
.collect(Collectors.toList())));
Output: 4
换句话说,您可以使用Collection.max(Stream.of(..).collect(Collectors.toList());
这里有一个例子
System.out.println(Collections.max(Stream.of(new int[][]{{1, 2}, {3, 4}})
.map(a -> Collections.max(Arrays.stream(a).boxed()
.collect(Collectors.toList())))
.collect(Collectors.toList())));
Output: 4
换句话说,您可以使用Collection.max(Stream.of(..).collect(Collectors.toList());
这里有一个例子
System.out.println(Collections.max(Stream.of(new int[][]{{1, 2}, {3, 4}})
.map(a -> Collections.max(Arrays.stream(a).boxed()
.collect(Collectors.toList())))
.collect(Collectors.toList())));
Output: 4
不,如果我从1开始内部循环的循环计数器,它将只在列中循环4次。它应该只从0开始。它会,但这将给出正确的答案-max
从第一个元素的值开始,然后将其与剩余的每个(4)进行比较值。无需将此值与第零个元素进行比较,因为它已被设置为等于它,所以保存一次循环迭代