数组拷贝及排序

复制代码

package com.test;
import java.util.*;

import static java.util.Arrays.fill;

public class Main {
public static void main(String[] args) {
int [] a = new int[5];
for(int i = 0; i < 5; i ++){
Scanner number = new Scanner(System.in);
int digit = number.nextInt();
a[i] = digit;
}
for(int i : a ){//用foreach语句进行遍历
System.out.println(i);
}
// int []copylucynumbers = Arrays.copyOf(a,2 * a.length);
// 拷贝数组需要用到Arrays类中的copyOf方法。
// 若数组元素是数值型,那么新增的元素将填入0;
// 如果数组元素是布尔型,则填入false。
// System.out.println(Arrays.toString(copylucynumbers));
// int []copylucybumbers2 = Arrays.copyOf(a,a.length-1);//相反如果长度小于原数组的长度,则只拷贝前面的值。
// System.out.println(Arrays.toString(copylucybumbers2));
Arrays.sort(a);//数组排序需要用到Arrays类中的sort方法,感觉有点类似C++中的sort函数
// System.out.println(Arrays.toString(a));
for(int i : a ){//用foreach语句进行遍历
System.out.println(i);
}
}
}
 
复制代码

 

posted @   弈星  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示