zoukankan      html  css  js  c++  java
  • JAVA_SE基础——36.static的实际应用

    什么时候定义静态函数

    如果功能内部没有访问到非静态数据(对象的特有数据。那么该功能就可以定义为静态)

    P.S.

    静态方法作为类和接口的重要组成部分,可以通过类名或接口直接访问,通常将那些使用频率较高的方法声明为静态方法,从而可以方便地使用这些方法,并能提高程序的开发效率和性能。


    我认为对于某些小功能的函数,而且内部没有访问到非静态数据,这种情况应该定义为静态,就如JavaAPI里面的方法摘要一样全部都是用static 关键字来修饰的。



    定义数组工具类:

    /*
    	定义数组工具类
    	1:定义一个遍历数组的函数
    	2:定义一个求数组和的功能函数  1. 遍历  2. 两两相加
    	3:定义一个获取数组最大值的功能函数
    	4:定义一个获取数组最大值角标的功能函数
    	5:定义一个返回指定数在指定数组中包含的角标的功能函数
    	6:定义一个可以用于排序int数组的函数
    		1:冒泡
    		2:选择	
     */
    class Arrays {
    
    	
    
    	// 1:定义一个遍历数组的函数
    	public static void print(int[] arr) {
    		for (int x = 0; x < arr.length; x++) {
    			if (x != (arr.length - 1)) {
    				System.out.print(arr[x] + ",");
    			} else {
    				System.out.print(arr[x]);
    			}
    
    		}
    	}
    
    	// 2:定义一个求数组和的功能函数
    	public static int getSum(int[] arr) {
    		int sum = 0;
    		for (int x = 0; x < arr.length; x++) {
    			sum += arr[x];
    		}
    		return sum;
    	}
    
    	// 3:定义一个获取数组最大值的功能函数
    	public static int getMax(int[] arr) {
    		int max = 0;
    		for (int x = 0; x < arr.length; x++) {
    			if (arr[max] < arr[x]) {
    				max = x;
    			}
    		}
    		return arr[max];
    	}
    
    	// 4:定义一个获取数组最大值角标的功能函数
    	public static int getIndexMax(int[] arr) {
    		int max = 0;
    		for (int x = 0; x < arr.length; x++) {
    			if (arr[max] < arr[x]) {
    				max = x;
    			}
    		}
    		return max;
    	}
    
    	// 5:定义一个返回 指定数在指定数组中包含的角标的功能函数
    	public static int getIndex(int[] arr, int src) {
    		int index = -1;
    		for (int x = 0; x < arr.length; x++) {
    			if (arr[x] == src) {
    				index = x;
    			}
    		}
    		return index;
    	}
    
    	// 冒泡
    	public static void test(int[] arr) {
    		for (int x = 0; x < arr.length - 1; x++) {
    			if (arr[x] > arr[x + 1]) {
    				int temp = arr[x + 1];
    				arr[x + 1] = arr[x];
    				arr[x] = temp;
    
    			}
    		}
    	}
    
    	// 选择排序
    	public static void selectSort(int[] arr) {
    		for (int x = 0; x < arr.length - 1; x++) {
    			for (int y = 1 + x; y < arr.length; y++) {
    				if (arr[x] > arr[y]) {
    					int temp = arr[y];
    					arr[y] = arr[x];
    					arr[x] = temp;
    				}
    			}
    		}
    	}
    
    	// 7:定义一个可以将整数数组进行反序的功能函数。
    	public static void reverseSort(int[] arr) {
    		int start = 0;
    		int end = arr.length - 1;
    		for (int x = 0; x < arr.length; x++) {
    			if (start < end) {
    				int tem = arr[start];
    				arr[start] = arr[end];
    				arr[end] = tem;
    			}
    			start++;
    			end--;
    		}
    
    	}
    
    	// 折半查找
    	public static int halfSearch(int key, int[] arr) {
    		int min = 0;
    		int max = arr.length - 1;
    		int mid = 0;
    
    		while (min < max) {
    			mid = (min + max) / 2;
    			if (key > arr[mid]) {
    				min = mid + 1;
    			} else if (key < arr[mid]) {
    				max = mid - 1;
    			} else {
    				return mid;
    			}
    		}
    		return -1;
    	}
    
    }
    
    class Demo3 {
    
    	public static void main(String[] args) {
    		int[] arr = { 1, 3, 2, 4};
    		Arrays.selectSort(arr);//选择排序
    		Arrays.print(arr);//遍历数组.
    		System.out.println();	
    		//获取数组最大值的功能函数
    		int x = Arrays.getMax(arr);
    		System.out.println(x);	
    	}
    }
    运行结果:





  • 相关阅读:
    SpannableString
    Java 之 FileReader FileInputStream InputStreamReader BufferedReader 作用与区别
    Java 之 File
    android 获取配置文件 相对路径
    Canvas
    Java Set List Map
    Comparable与Comparator区别
    HashMap HashTable ConcurrentHashMap
    centos7 mongodb 3.4 yum 安装
    对于JAVA程序优化的一些想法,读书有感.治疗强迫症良药
  • 原文地址:https://www.cnblogs.com/Jhaiha0/p/8465305.html
Copyright © 2011-2022 走看看