1. Variable parameters
Requirements: To a design method for an arbitrary integer parameter addition result calculation, early implementations (array):
public class Test{
public static void main(String[] args) {
System.out.println(add(new int[]{1}));
System.out.println(add(new int[]{1,2,3}));
System.out.println(add(new int[]{1,4,6}));
System.out.println(add(new int[]{1,1,3,4,4}));
}
public static int add(int[] data){
int result = 0;
for (int i = 0; i < data.length; i++) {
result += data[i];
}
return result;
}
}
JDK1.5 added after 可变参数
the concept defined in the following format:
publlic [static] [final] 返回值 方法名(参数类型 参数名, 参数类型...参数名称){}
The method of the variable parameters to achieve the above requirements:
public class Test{
public static void main(String[] args) {
System.out.println(add(1,3,4));//随意传递的内容,随意个数
System.out.println(add(new int[]{1,2,3}));//可变参数可以接收数组
System.out.println(add(new int[]{1,4,6}));
System.out.println(add(new int[]{1,1,3,4,4}));
}
public static int add(int...data){
int result = 0;
for (int i = 0; i < data.length; i++) {
result += data[i];
}
return result;
}
}
可变参的本质上是数组,一个方法中只允许一个可变参,并且可变参一定是方法最后一个参数
, Many types of transmission parameters Example:
public class Test{
public static void main(String[] args) {
System.out.println(add("Hello"));
System.out.println(add("Hello",1,4,5,6));
System.out.println(add("Hello",new int[]{1,2,3}));
}
public static int add(String msg,int ... data) {
int result = 0 ;
for (int i = 0; i < data.length; i++) {
result += data[i] ;
}
return result ;
}
}
2.foreach (Enhanced for loop)
Used for the original array output cycle is complete, for example:
public class Test{
public static void main(String[] args) {
int[] data = new int[] { 1, 2, 3, 4, 5 }; // 原始数组
for (int i = 0; i < data.length; i++) {
System.out.println(data[i]); // 通过循环控制索引下标
}
}
}
foreach syntax:
for(数据类型 临时变量 : 数组(集合)){
//循环次数为数组长度,而每一次循环都会顺序取出数组中的一个元素赋值给临时变量
}
Examples are as follows:
public class Test {
public static void main(String[] args) {
int[] data = new int[] { 1, 2, 3, 4, 5 }; // 原始数组
for (int i : data) { // 将数组中每个元素赋值给i
System.out.println(i); // 这种循环避免了角标的问题
}
}
}
3. static imports
MyMath categories:
package www.xpu.java.util;
public class MyMath {
public static int add(int x, int y) {
return x + y;
}
public static int sub(int x, int y) {
return x - y;
}
}
MyMath first import the class, then all the static MyMath class by calling
methods:
package www.xpu.java.test;
import www.xpu.java.util.MyMath;
public class TestDemo {
public static void main(String[] args) {
System.out.println(MyMath.add(10, 20));
System.out.println(MyMath.sub(30, 10));
}
}
JDK1.5 start from, all the static field of a class (static properties including methods), all classes into use, properties and methods to call at this time is no longer required class name can be called directly main methods:
package www.xpu.java.test;
import static www.xpu.java.util.MyMath.*; // 静态导入
public class TestDemo {
public static void main(String[] args) {
System.out.println(add(10, 20));
System.out.println(sub(30, 10));
}
}
In fact this method is not efficient, the development is not recommended