Java数组的打印 ,最大值,最小值。动态静态初始化

class Array
{
static int getMax(int[] nums)
{
int max = nums[0]; //假设最大的一个是第一个
for (int index = 0;index<nums.length ;index++ )
{
if (max<=nums[index])
{
max = nums[index]; //把最大值存储在max中
}

	}
	return max;
}

static int getMin(int[] nums)
{
	int min = nums[0]; //假设最小值是第一个
	for (int index = 1;index<nums.length ;index++ )
	{
		if (min>=nums[index])
		{
			min = nums[index];
		}
	}
	return min;

}

public static void main(String[] args) 
{
	/**
	数组的静态初始化
	int[] num = new int[]{1,2,3,5};
	// 数组的简单写法 必须声明立刻初始化  int[] num = {1,2,3};
	System.out.println(num.length);

	*/
	/**
	数组的动态初始化 即 事先给数组长度
	int[] nums = new int[5];
	System.out.println(nums.length);

	nums = new int[8];
	System.out.println(nums.length);


	int[] nums = new int[]{1,3,4,8};
	System.out.println("打印数组长度"+nums.length);
	System.out.println("打印数组第一个元素"+nums[0]);
	System.out.println("打印数组的最后一个元素"+nums[3]);
	//修改数组中的某个元素
	nums[0] = 1000;
	System.out.println("打印数组第一个元素"+nums[0]);

	//使用循环遍历。打印数组中的元素
	for (int index = 0;index<nums.length ;index++ )
	{
		System.out.print(nums[index]+"  ");
	}
	*/

//获取数组中最大 getMax() 最小的数值 getMin()
    int[] nums = new int[]{1,3,5,40}; 
	int max = Array.getMax(nums);
	System.out.println(max);
	System.out.println(Array.getMin(nums));


}

}

原文地址:https://www.cnblogs.com/thttt/p/11637634.html