import java.util.Arrays;
public class MyDemo {
public static void main(String[] args) {
int[][] arys = { { 1, 2, 3 }, { 4, 5 }};//二维数组
int[] min = new int[arys.length];//一位数组的长度是和二维数组的长度一样
for (int i = 0; i < arys.length; i++) {//外循环, 每次是一行
int mintemp = arys[i][0];// 得到第i行的下标为0个元素假设他是最小值
for (int j = 1; j < arys[i].length; j++) {//然后从下标是1的元素开始循环比较
int temp = arys[i][j];
if(mintemp>temp){//如果比假设的最小值还要小. 那么就设置temp元素为最小值
mintemp=temp;
}
}
min[i]=mintemp;//存入一维数组中
}
System.out.print("{");//打印左边的花括号
for (int i = 0; i < min.length-1; i++) {//只循环到倒数第二个元素位置
System.out.print(min[i]+",");//输出元素,并追加逗号
}
System.out.print(min[min.length-1]+"}");//倒数第一个元素后面不需要追加逗号
//System.out.println("\n"+Arrays.toString(min)); 输出[1,4]
}
}
运行测试
{1,4}
结果截图
// 这样差不多满足要求了吧?
import java.util.Arrays;
public class GoGoGo {
public static void main(String[] args) {
int[][] arr2 = { {2, 1, 3 }, { 4, 5 }, {12, 111, 23, 999, 8, 998 } };
int[] result = new int[arr2.length];
int index = 0;
for (int[] arr : arr2) {
Arrays.sort(arr);
result[index++] = arr[0];
}
for (int x : result) {
System.out.println(x);
// Console :
// 1
// 4
// 8
}
}
}
public class Egg{
public static void main(String[] args){
int[][] arr = {
{1,2,3},
{4,5}
};
int[] dest = new int[0];
for(int i = 0; i < arr.length; i++){
int min = Integer.MAX_VALUE;
for(int j = 0; j < arr[i].length; j++){
min = arr[i][j] < min ? arr[i][j] : min;
}
int[] cloned = new int[dest.length + 1];
System.arraycopy(dest,0,cloned,0,dest.length);
cloned[cloned.length - 1] = min;
dest = cloned;
}
for(int i = 0; i < dest.length; i++){
System.out.print(dest[i] + " ");
}
}
}
int[][] a = {{4,5,2},{12,56,9}};
List list = new ArrayList();
int mm;
for (int i = 0; i < a.length; i++) {
mm = a[i][0];
for (int j = 0; j < a[i].length; j++) {
System.out.println(a[i][j]);
if(a[i][j]
}
}
list.add(mm);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}