A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

定义一个二维int数组,编写代码获取最小元素。

15 个回复

倒序浏览
  1. package com.test;

  2. public class GetMin {
  3.         public static void main(String[] args) {
  4.                 int[] arr = new int[]{1,3,5,6,2,7,10};
  5.                 System.out.println(getMin(arr));
  6.         }
  7.         private static int getMin(int[] arr){
  8.                 int min = arr[0];
  9.                 for (int i = 1; i < arr.length-1; i++) {
  10.                         if(arr[i]<min){
  11.                                 min=arr[i+1];
  12.                         }
  13.                 }
  14.                 return min;
  15.         }
  16. }
复制代码
回复 使用道具 举报 1 0
刚才那个是一维数组的,现在这个是二维数组的
  1. package com.test;

  2. public class GetMin {
  3.         public static void main(String[] args) {
  4.                 int[][] arr = new int[][]{{1,2,5},{4,5,3},{10,3,5},{6,3,1},{1,2,8},{6,0,7},{-1,20,10}};
  5.                 System.out.println(getMin(arr));
  6.         }
  7.         private static int getMin(int[][] arr){
  8.                 int min = arr[0][0];
  9.                 for (int i = 0; i < arr.length; i++) {
  10.                         for (int j = 0; j < arr[i].length; j++) {
  11.                                 if(arr[i][j]<min){
  12.                                         min=arr[i][j];
  13.                                 }
  14.                         }
  15.                 }
  16.                 return min;
  17.         }
  18. }
复制代码
回复 使用道具 举报
定义变量,循环遍历,
回复 使用道具 举报
  1. package array;

  2. /**
  3. * @author 小媛
  4. * 求二维数组中的最大值\最小值
  5. *
  6. */
  7. public class ArrayMax {
  8.         public static void main(String[] args) {
  9.                 int[][] arr = { { 6, 2, 3, 8, 11, 1 }, { 7, 1, 5, 4, 2 } };

  10.                 System.out.println(getMax(arr));
  11.                 System.out.println(getMin(arr));

  12.         }

  13.         private static int getMax(int[][] arr) {
  14.                 int max = 0;

  15.                 for (int i = 0; i < arr.length; i++) {

  16.                         for (int j = 0; j < arr[i].length; j++) {
  17.                                 if (arr[i][j] > max) {
  18.                                         max = arr[i][j];
  19.                                 }
  20.                         }
  21.                 }

  22.                 return max;
  23.         }

  24.         private static int getMin(int[][] arr) {
  25.                 int min = 2147483647;//设置为int最大,不用担心负数了

  26.                 for (int i = 0; i < arr.length; i++) {
  27.                         for (int j = 0; j < arr[i].length; j++) {
  28.                                 if (arr[i][j] < min) {
  29.                                         min = arr[i][j];
  30.                                 }
  31.                         }
  32.                 }
  33.                 return min;
  34.         }
  35. }
复制代码


以上是小媛的思路。虽然感觉有点牵强直接用int 的最大来做初始值。主要还是为了避免可能出现的数组中存在负数的元素吧。
不知道其他大神还有没有更好的解决方法
回复 使用道具 举报 1 0
加油,楼主威武,不要问我叫什么,我叫雷锋
回复 使用道具 举报
package com.test;

public class GetMin {
        public static void main(String[] args) {
                int[] arr = new int[]{1,3,5,6,2,7,10};
                System.out.println(getMin(arr));
        }
        private static int getMin(int[] arr){//设置成方法
                int min = arr[0];
                for (int i = 1; i < arr.length-1; i++) {//循环
                        if(arr[i]<min){
                                min=arr[i+1];
                        }
                }
                return min;//返回最小值
        }
}
回复 使用道具 举报 1 0
李志慧 发表于 2015-11-4 23:03
package com.test;

public class GetMin {

互利互惠,挣黑马币
回复 使用道具 举报
来的太晚
回复 使用道具 举报
定义一个临时变量,让其中的值去比较必将最小的赋给它
回复 使用道具 举报
hhl 中级黑马 2015-11-8 23:12:01
11#
不错哟,下面给出的答案都很全面
回复 使用道具 举报
6666666666666666666
回复 使用道具 举报
本帖最后由 黑夜中那颗星 于 2015-11-9 12:15 编辑
  1. public class Test {
  2.         public static void main(String[] args) {
  3.                 int[][] arr = {{4,5,6},{99,88,5},{1,3,7},{66,55,7}};
  4.                 System.out.println(getMin(arr));
  5.                
  6.         }
  7.         public static int getMin(int[][] arr){
  8.                 int min = arr[0][0];
  9.                 for(int x = 0;x<arr.length;x++){
  10.                         for(int y = 0;y<arr[x].length;y++){
  11.                                 min = min<arr[x][y]?min:arr[x][y];
  12.                         }
  13.                 }
  14.                 return min;
  15.         }
  16. }
复制代码

回复 使用道具 举报
个人喜欢返回最小元素角标
回复 使用道具 举报
这样写也可以.
回复 使用道具 举报
我是来看看的
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马