java数组操作
main方法中的参数数组String[] args
package indi.exer.yuluo.collection.arrays;
import java.util.Arrays;
public class test1 {
public static void main(String[] args) {
String username = args[0];
String password = args[1];
if (username.equals("admin") && password.equals("123")) {
System.out.println("登陆成功,欢迎 [" + username +"]回来");
System.out.println("继续使用此系统……");
} else {
System.out.println("输入的用户名或密码不正确,请重新输入");
}
System.out.println("args数组的长度:" + args.length + "args数组的内容:" + Arrays.toString(args));
}
}
数组的扩容和拷贝
package indi.exer.yuluo.collection.arrays;
public class test2 {
public static void main(String[] args) {
int[] src = {1, 2, 3, 4, 5};
int[] array = new int[10];
System.arraycopy(src, 1, array, 4, 3);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + "\t");
}
}
}
Array的常用方法
使用了apache的commons lang3工具类
package indi.exer.yuluo.collection.arrays;
import com.sun.deploy.util.ArrayUtil;
import org.apache.commons.lang3.ArrayUtils;
import java.util.ArrayList;
import java.util.Arrays;
public class test3 {
public static void main(String[] args) {
int[] arrays = new int[10];
int[] array = {1, 4, 6, 3, 8, 2, 10, 19};
int[] array1 = {20, 23, 28};
Arrays.sort(array);
System.out.println(Arrays.toString(array));
String[] stringArray = {"yuluo", "huakai"};
ArrayList<String> str = new ArrayList<>(Arrays.asList(stringArray));
str.forEach(System.out::println);
System.out.println(Arrays.asList(stringArray).contains("yuluo"));
String s = ArrayUtil.arrayToString(stringArray);
System.out.println(s);
int[] ints = ArrayUtils.addAll(array, array1);
for (int i = 0; i < ints.length; i++) {
System.out.print(ints[i] + "\t");
}
ArrayUtils.reverse(stringArray);
for (int i = 0; i < stringArray.length; i++) {
System.out.println("stringArray[i] = " + stringArray[i]);
}
}
}
|