面向对象——数组工具类的抽取

工具类:代码中重复使用的一些功能,重复写显得繁琐费时,抽取出来,创建一个专门存放这写功能的类,需要用到这些类时,只需要主函数调用就行。

/*
获得数组的最大值
*/
public class ArrayTool
{
		public static  void int getMax(int[] arr)
	{
		int maxIndex = 0;
		for(int x=1; x<arr.length; x++)
		{
			if(arr[x]>arr[maxIndex])
			maxIndex = x;//
		}

		return arr[maxIndex];
	}
			
}

主函数调用方法:

class ArrayToolDemo 
{
	/*
	保证程序的独立运行。
	*/
	public static void main(String[] args) 
	{
		int[] arr = {4,8,2,9,72,6};

		int index = ArrayTool.getIndex(arr,8);
		System.out.println("index="+index);
	}

}

猜你喜欢

转载自blog.csdn.net/zhanshixiang/article/details/81189953