栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

01-java数组的应用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

01-java数组的应用

数组概念

数组是先声明,在开辟空间的,但是开辟完空间后长度是固定的
所以数组一旦创建完成之后,它的长度就不变了
数组没有赋值,但是会有个默认值
* int 类型默认值是 0
* double 是 0.00
* boolean 是false
* String 是 null

public class Demo01 {
    public static void main(String[] args) {
        
        
        Random random = new Random();
        int[] nums = new int[10];
        int i = 0;
        while (true) {
            int num = random.nextInt(1000);
            if(num%3 == 0){
                nums[i] = num;
                i++;
                System.out.println(Arrays.toString(nums));
            }
            if(i == 10){
                break;
            }
        }
    }
}
数组与基本数据类型的区别

实现数组的添加数据
 @Test
    public void test01(){
        
        
        // 目标数组
        int[] target;
        // 原数组
        int[] source = {1, 12, 23, 34};
        // 需要插入的数据
        int num = 56;
        // 记录要插入的位置
        int index = -1;
        //判断出新的数组应该在那个位置
        for (int i = 0; i < source.length; i++) {
            if(num <= source[i]){
                index = i; //找到位置,跳出循环
                break;
            }
        }
        if(index == -1){
            //在数组的最后以为插入
            target = Arrays.copyOf(source, source.length + 1); //把原数组赋值给新数组,并扩容一位
            target[source.length] = num; //并在最后一位插入数据
        }else{
            //在中间插入数据
            target = new int[source.length + 1];
            //新数组的赋值
            for(int i =0;i
                //如果i < 要插入的位置 那么就全部赋值给位置
                if(i < index){
                    target[i] = source[i];
                }else if(i == index){
                    target[i] = num;
                }else if(i > index){
                    target[i] = source[i - 1];
                }
            }
        }
        System.out.println(Arrays.toString(target));
    }
通过Arrays和System来实现数据添加
//案例
        int[] target = new int[20];
        int[] source = {1, 12, 23, 34,45,56,67,78};
        int num = 40;
        int index = 5;
        System.arraycopy(source,0,target,0,5);
        target[index] = num;
        System.arraycopy(source,index,target,index+1,source.length - index);
        System.out.println(Arrays.toString(target));
@Test
    public void test02(){
        
        // 目标数组
        int[] target;
        // 原数组
        int[] source = {1, 12, 23, 34};
        // 需要插入的数据
        int num = 2;
        // 记录要插入的位置
        int index = -1;
        //判断出新的数组应该在那个位置
        for (int i = 0; i < source.length; i++) {
            if(num <= source[i]){
                index = i; //找到位置,跳出循环
                break;
            }
        }
        if(index == -1){
            //在数组的最后以为插入
            target = Arrays.copyOf(source, source.length + 1); //把原数组赋值给新数组,并扩容一位
            target[source.length] = num; //并在最后一位插入数据
        }else{
            //在中间插入数据
            target = new int[source.length + 1];
            //新数组的赋值
            //index前面赋值
            System.arraycopy(source,0,target,0,index);
            //index赋值
            target[index] = num;
            //index
            System.arraycopy(source,index,target,index+1,source.length - index);
        }
        System.out.println(Arrays.toString(target));

    }
数组的冒泡排序
    
    @Test
    public void test03(){
        int[] nums = {1, 34, 23, 12, 90, 35};
        int temp = 0;
        for(int i=0; i
            for(int j = 0; j< nums.length - 1 - i; j++){
                //前面一个数大于后面一个数交换位置
                if(nums[j] > nums[j+1]){
                    temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(nums));
    }
数组案例实操

    
    public Boolean checkStringArray(String[] names){
        for (String name : names) {
            if ("".equals(name) || name == null) {
                return true;
            }
        }
        return false;
    }
 @Test
    public void test04(){
        String[] names = {"hehe", "zhangsan", "lisi", "wangcai"};
        String[] newNames; //扩展数组
        String next;
        Arrays.toString(names);
        System.out.println(Arrays.toString(names));
        //现在就输入字符串
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入数据:");
            next = scanner.next();
            //判断names是否有空串
            if(checkStringArray(names)){
                //判断第几个为空
                for(int i=0; i
                    if(names[i] == null || names[i].equals("")){
                        names[i] = next;
                        break;
                    }
                }
            }else{
                //字符串不为空。动态扩容50%
                newNames = new String[names.length + (Integer)names.length/2];
                System.arraycopy(names, 0, newNames, 0, names.length);
                for(int i =0;i
                    if(newNames[i] == null || newNames[i].equals("")){
                        newNames[i] = next;
                        break;
                    }
                }
                //直接覆盖原来的数组
                names = newNames;
            }
            System.out.println(Arrays.toString(names));
        }
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/871622.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号