| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2359 人关注过本帖
标题:创建方法 add(String[] strList ,int index, String str ){方法自己写}// ...
只看楼主 加入收藏
Leisurely
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2017-2-20
结帖率:0
收藏
已结贴  问题点数:20 回复次数:10 
创建方法 add(String[] strList ,int index, String str ){方法自己写}//用于向strList的index位置插入s
java数据结构:创建方法 add(String[] strList ,int index, String str ){方法自己写}//用于向strList的index位置插入str。
请各位大佬帮忙 多谢
搜索更多相关主题的帖子: 方法 str String index 位置 
2018-04-04 11:09
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:7 
程序代码:
package com.xiaoa.demo;

/*java数据结构:创建方法 add(String[] strList ,int index, String str ){方法自己写}//用于向strList的index位置插入str。
请各位大佬帮忙 多谢 */
public class ArrayTest {
    public static void main(String[] args) {
        String[] strList = new String[] { "你", "好" };
        String[] strList2 = add(strList, 2, "啊");
        for (String string : strList2) {
            System.out.println(string);
        }
    }

    public static String[] add(String[] strList, int index, String str) {
        String[] strList2 = new String[strList.length+1];
        for(int i=0;i<strList2.length;i++) {
            if(i<index) {
                strList2[i]=strList[i];
            }
            if(i==index) {
                strList2[i] = str;
            }
            if(i>index) {
                strList2[i]=strList[i-1];
            }
        }
       

        return strList2;

    }
}

假如人生没有梦想,和咸鱼有什么区别!
2018-04-05 11:30
桃花岛主丶
Rank: 2
等 级:论坛游民
威 望:3
帖 子:27
专家分:58
注 册:2018-3-20
收藏
得分:7 
public class addDemo {
    public static void main(String[] args) {
        String []strList={"a","c","d"};
        int index=1;
        String str="b";
        String []newStrList=add(strList ,1,"b");
        for (String st : newStrList) {
            System.out.print(st+" ");
        }
    }

    public static String[] add(String[] strList, int index, String str) {
        ArrayList<String> arr = new ArrayList<String>();
        for (int i = 0; i <=strList.length; i++) {
            if (i<index) {
                arr.add(strList[i]);
            }else if(i==index){
                arr.add(str);
            }else {
                arr.add(strList[i-1]);
            }
        }
        String[] newstrList = arr.toArray(strList);
        return newstrList;
    }
}
2018-04-05 14:56
桃花岛主丶
Rank: 2
等 级:论坛游民
威 望:3
帖 子:27
专家分:58
注 册:2018-3-20
收藏
得分:0 
public class addDemo {
    public static void main(String[] args) {
        String []strList={"a","c","d"};
        String []newStrList=add(strList ,1,"b");
        for (String st : newStrList) {
            System.out.print(st+" ");
        }
    }

    public static String[] add(String[] strList, int index, String str) {
        ArrayList<String> arr = new ArrayList<String>();
        for (int i = 0; i <=strList.length; i++) {
            if (i<index) {
                arr.add(strList[i]);
            }else if(i==index){
                arr.add(str);
            }else {
                arr.add(strList[i-1]);
            }
        }
        String[] newstrList = arr.toArray(strList);
        return newstrList;
    }
}
再删一点...
2018-04-05 14:57
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:7 
回复 4楼 桃花岛主丶

剑栈风樯各苦辛,别时冰雪到时春
2018-04-05 18:06
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:0 
回复 5楼 林月儿
你来个更短的呗

假如人生没有梦想,和咸鱼有什么区别!
2018-04-05 19:20
林月儿
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:138
帖 子:2277
专家分:10647
注 册:2015-3-19
收藏
得分:0 
以下是引用疯狂的小a在2018-4-5 19:20:41的发言:

你来个更短的呗


俺はこれを上手ではありませんよう

剑栈风樯各苦辛,别时冰雪到时春
2018-04-05 22:43
桃花岛主丶
Rank: 2
等 级:论坛游民
威 望:3
帖 子:27
专家分:58
注 册:2018-3-20
收藏
得分:0 
回复 5楼 林月儿
版主好.握个爪~
2018-04-06 23:50
桃花岛主丶
Rank: 2
等 级:论坛游民
威 望:3
帖 子:27
专家分:58
注 册:2018-3-20
收藏
得分:0 
我又来了,集合有添加方法,忘了用,罪过...
public class Demo {
    public static void main(String[] args) {
        String []strList={"a","c","d"};
        String []newStrList=add(strList ,1,"b");
        for (String st : newStrList) {
            System.out.print(st+" ");
        }
    }

    public static String[] add(String[] strList, int index, String str) {
        ArrayList<String> arr = new ArrayList<String>();
        for (int i = 0; i < strList.length; i++) {
            arr.add(strList[i]);
        }
        arr.add(index, str);
        String[] newstrList = arr.toArray(strList);
        return newstrList;
    }
}
2018-04-07 00:02
疯狂的小a
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:39
帖 子:423
专家分:1871
注 册:2018-2-6
收藏
得分:0 
回复 9楼 桃花岛主丶
厉害了

假如人生没有梦想,和咸鱼有什么区别!
2018-04-07 09:05
快速回复:创建方法 add(String[] strList ,int index, String str ){方法自己 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016764 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved