zoukankan      html  css  js  c++  java
  • 数组添加:如何往数组的"null"位置插入数据呢?

    数组添加,当已经存在的一个数组时,如何往数组的"null"位置插入数据呢?

    分析:

    1、循环遍历数组元素,找出null的位置(下标)

    2、设置一个变量,接收null位置下标值

    3、赋值给此位置

    4、循环输出数组看结果

    public class AddNum {
        public static void main(String[] args) {
            //先定义一个数组
            String[] phones={"iphone4","iphone4s","iphone5",null};
            
            //查找位置
            int index=-2;
            String addPhone="iphone5s";
            for(int i=0;i<phones.length;i++){
                if(phones[i]==null){
                    index=i;
                    break;
                }
            }
            System.out.println("index="+index);
            
            
            //插入即赋值
            phones[index]=addPhone;
            for(int phone=0;phone<phones.length;phone++){
                System.out.print(phones[phone]+"	");
            }
        }
    }
  • 相关阅读:
    认识dojo
    CommonJS规范
    点滴
    快速排序
    npm常用命令
    http详解
    js经验点滴js apply/call/caller/callee/bind使用方法与区别分析
    给string添加新的函数
    大马隐藏锁定研究
    一键购买
  • 原文地址:https://www.cnblogs.com/sincoolvip/p/5444387.html
Copyright © 2011-2022 走看看