qq_38642824
_add
采纳率33.3%
2017-07-19 03:17

java集合截取按照int数组里面的数值截取

2
已采纳

List s = ["aa","sss","ddd","fff","ggg","hhhh"]
int[] a = [3,1,2]
//怎么把s按照数组a截取如下:
["aa","sss","ddd"]
["fff"]
["ggg","hhhh"]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

8条回答

  • guaoran guaoran 4年前

    public static void main(String[] args) {
    String[] arrStr = {"aa","sss","ddd","fff","ggg","hhhh"};
    //转换成list,可以通过下标删除元素
    List list = Arrays.asList(arrStr);
    /** 转换成ArrayList
    * Arrays.asLisvt() 返回java.util.Arrays$ArrayList, 而不是ArrayList。
    * Arrays$ArrayList和ArrayList都是继承AbstractList,remove,add等
    * method在AbstractList中是默认throw UnsupportedOperationException而且不作任何操作。 ArrayList override这些method来对list进行操作,
    * 但是Arrays$ArrayList没有override remove(int),add(int)等,所以throw UnsupportedOperationException
    * 所以需要转换成ArrayList
    */
    List arrayList = new ArrayList(list);
    //如果是list的话,就不需要以上的步骤了。
    int[] a = {3,1,2};
    for (int i = 0; i < a.length; i++) {
    int num = a[i];
    //初始化数组长度
    String[] arr = new String[num] ;
    //进行转成成数组,删除已经复制过的元素
    for (int j = 0; j < num; j++) {
    arr[j] = arrayList.get(0);
    arrayList.remove(0);
    }
    System.out.println(Arrays.toString(arr));
    }
    }

    点赞 1 评论 复制链接分享
  • bikaichuancs BKCPRO 4年前

    我的方法效率会比较高,时间复杂度O(n)

    public static void main(String[] args) {

        String[] s = {"aa", "sss", "ddd", "fff", "ggg", "hhhh"};
        int[] a = {3, 1, 2};
    
        List<String> list = Arrays.asList(s);
    
        int intLen = a.length;
    
        int index = 0;
        for (int i = 0; i < intLen; i++) {
            System.out.println(list.subList(index,index + a[i]));;
            index = a[i];
        }
    }
    
    点赞 评论 复制链接分享
  • u012824078 八戒吃荤不吃素 4年前

    亲测可以得到你想要的结果,麻烦采纳
    public static void main(String[] args) {
    String[] s = {"aa","sss","ddd","fff","ggg","hhhh"};
    int[] a = {3,1,2};
    int start = 0;
    ArrayList list = new ArrayList();
    for(int j=0;j<a.length;j++){
    int len = a[j];
    for(int i=start;i<(len+start);i++){
    list.add(s[i]);
    }
    start = start + len;
    System.out.println(list);
    list.clear();
    }
    }

    点赞 评论 复制链接分享
  • hl233211 Planet1987 4年前
        List<String> s = Arrays.asList("aa", "sss", "ddd", "fff", "ggg", "hhhh");
        int[] a = new int[]{3, 1, 2};
    
        List<List<String>> result = new ArrayList<>();
        int lastIdx = 0;
        for (int idx = 0; idx < a.length; idx++) {
            if (lastIdx >= s.size()) {
                break;
            }
            result.add(s.subList(lastIdx, Math.min(lastIdx + a[idx], s.size()))); // 考虑越界异常
            lastIdx += a[idx];
        }
        for (List<String> sl : result) {
            System.out.println(sl);
        }
    
    点赞 评论 复制链接分享
  • lxcymklc lxcymklc 4年前

    String []s = {"aa","sss","ddd","fff","ggg","hhhh"};
    int[] a = {3,1,2};
    int index = 0;
    for (int i = 0; i < a.length; i++) {
    String[] str = new String[a[i]];
    for (int j = 0; j < str.length; j++) {
    str[j] = s[j+index];
    }
    index+=a[i];
    System.out.println(str);
    }

    点赞 评论 复制链接分享
  • chenhuanchuan 就是哥 4年前

    public class dddd {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //
    String[] s = {"aa","sss","ddd","fff","ggg","hhhh"};
    int[] a = {3,1,2};
    int start=0;
    int end=0;
    for(int i=0;i<a.length;i++){
    end=a[i]+start;
    if(start==s.length){
        break;
    }
    String[] b=new String[a[i]];
    for(int y=start;y<end;y++){
    b[y-start]=s[y];    
    }
    show(b);
    start=end;
    }
    
    }
    
    private static void show(String[] b) {
        // TODO Auto-generated method stub
        String s ="[";
        for(int i=0;i<b.length;i++){
        if(i==0){
            s=s+b[i];
        }else{
            s=s+","+b[i];   
        }       
        }
        s+="]";
        System.out.println(s);
    }
    

    }

    点赞 评论 复制链接分享
  • u014074697 张大教主 4年前
    List<List<String>>resultList=new ArrayList<List<String>>();
    int start=0;
    for(int len:a){
        List<String>res=new ArrayList<String>();
        for(int i=start;i<(start+len);i++){
            if(i<s.size()){
                res.add(s.get(i));
            }
        }
        resultList.add(res);
        start+=len;
    }
    //resList结果就是[["aa","sss","ddd"],["fff"],["ggg","hhhh"]]遍历就可以挨个获取
    
    点赞 评论 复制链接分享
  • java2333 java2333 4年前

    list是这么用的吗? 那个s 应该是个String数组才对吧?

    点赞 评论 复制链接分享

相关推荐