拷贝 arraycopy
String [] s={"北京","上海","广州","深圳","成都"};
String [] ss=new String[6];
System.arraycopy(s,0,ss,0,s.length);
for(String d : ss) {
System.out.print(d+"\t");
}
工具类 查看数组元素 Arrays.toString()
import java.util.Arrays;//需要导入包
public class text001 {
public static void main(String[] args) {
int [] a={1,3,4,7,5,34,8};
System.out.println(Arrays.toString(a));
}
}
排序 Arrays.sort()
import java.util.Arrays;
public class text001 {
public static void main(String[] args) {
int [] a={1,3,4,7,5,34,8};
System.out.println(Arrays.toString(a));
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
使用Arrays类实现二分查找法 Arrays.binarySearch()
import java.util.Arrays;
public class text001 {
public static void main(String[] args) {
int [] a={1,2,3,34,355,233,444};
System.out.println(Arrays.toString(a));
Arrays.sort(a);//使用二分法查找,必须先对数组进行排序
System.out.println(Arrays.toString(a));
//返回排序后索引位置,若没有找到返回负数
System.out.println(""+Arrays.binarySearch(a,3));
}
}
使用Array类对数组进行填充 Arrays.fill()
import java.util.Arrays;
public class text001 {
public static void main(String[] args) {
int [] a={1,2,3,34,355,233,444};
System.out.println(Arrays.toString(a));
Arrays.fill(a,2,4,10);//2到4索引不包含4
System.out.println(Arrays.toString(a));
}
}
结果