简体   繁体   English

如何将值拆分为不同的数组?

[英]How can i split values in different arrays?

I am doing a project in android ,and i have a problem My array list values like this 我在android中做一个项目,而我有一个问题我的数组列表值是这样的

[men-1, men-2, men-3, women-1, women-2, women-3]

How can I split these values in different arrays? 如何将这些值拆分为不同的数组? ie how can I save men-1,men-2,men-3 in one array and women-1,women-2,women-3 in other array? 即我怎样才能在一个阵列中保存men-1,men-2,men-3在另一个阵列中保存women-1,women-2,women-3

It is hard to help you without details but you should probably 没有细节很难帮助您,但您应该

  1. create separate Lists for man and woman elements, 为男人和女人元素创建单独的列表,
  2. iterate over all elements of your lists 遍历列表的所有元素
    1. in case of list is string you can determine its gender using element.startsWith("man") 如果列表是字符串,则可以使用element.startsWith("man")确定其性别
    2. in case elements are objects of Man or Woman classes you can use instanceof operator like if (element instanceof Man){listOfMans.add(element)} 如果元素是男人或女人类的对象,则可以使用instanceof运算符,例如if (element instanceof Man){listOfMans.add(element)}
  3. now if you want to place ell elements in separate arrays you can use toArray(T[] array) method from each List instance. 现在,如果要将ell元素放置在单独的数组中,则可以使用每个List实例中的toArray(T[] array)方法。

the below code may help you 下面的代码可能会帮助您

import java.util.Arrays;
public class Test{
    public static void main(String[] args) {
       String[] input = new String[] {"men-1", "men-2", "men-3", "women-1", "women-2", "women-3"};
       int arr1Size=getCount(input, "women");
       String[] arr1 = new String[arr1Size];
       String[] arr2 = new String[input.length-arr1Size];
       int i=0,j=0;
       for(String str: input){
           if(str.contains("women")){
               arr1[i] = str;
               i++;
           }else{
               arr2[j] = str;
               j++;
           }           
       }
       System.out.println(Arrays.deepToString(arr1));
       System.out.println(Arrays.deepToString(arr2));       
    }

    public static int getCount(String[] input, String find){
        int count=0;
        for(String str: input){
           if(str.contains(find)){
               count++;
           }
        }
        return count;
    }
}

If those are the object of a class then you can easily do it using instanceOf operator... Otherwise below code can help you.. you can do it using arrays only as well as using arraylist 如果这些是类的对象,则可以使用instanceOf运算符轻松地进行操作...否则下面的代码可以为您提供帮助。.您可以仅使用数组以及使用arraylist进行操作

   /**
 * 
 */
package com.sharique.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author Sharique
 *
 */
public class Test {

    /**
     * @param args
     */
    //List<Integer> list = new ArrayList<>();
    //int[] arr = new int[10];
    //int[] ar1= arr.clone();

    public void usingArrays(){
        StringBuffer sb1 = new StringBuffer("Men");
        String[] arrays = {"Men-1","Men-2","Men-3","Women-1","Women-2","Women-3"};
        String[] arrOfMen = new String[3];
        String[] arrOfWomen = new String[3];
        String str=null;
        int count1=0;
        int count2=0;
        for(int i=0;i<arrays.length;i++){
            str=arrays[i];
            if(str.contains(sb1)){
                arrOfMen[count1++]=str;
            }else{              
                arrOfWomen[count2++]=str;
            }
        }
        for(int i=0;i<arrOfMen.length;i++)
            System.out.print(arrOfMen[i]+" ");
        //For a new Line
        System.out.println();
        for(int i=0;i<arrOfWomen.length;i++)
            System.out.print(arrOfWomen[i]+" ");
    }
    public void usingArrayList(){
        String[] arrays1 = {"Men-1","Men-2","Men-3","Women-1","Women-2","Women-3"};
        List<String> list = new ArrayList<>(Arrays.asList(arrays1));
        String[] arrays2 = new String[3];
        StringBuffer sb = new StringBuffer("Women");
        int count=0;
        String str=null;
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()){
            str =iterator.next();
            if(str.contains(sb)){
                arrays2[count++] = str;
                iterator.remove();
            }
        }
        //for a new line
        System.out.println();
        arrays1 = list.toArray(new String[list.size()]);
        for(int i=0;i<arrays1.length;i++)
            System.out.print(arrays1[i]+" ");
        //for a new line
        System.out.println();
        for(int i=0;i<arrays2.length;i++)
            System.out.print(arrays2[i]+" ");
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Test test = new Test();
        test.usingArrays();
        test.usingArrayList();

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不同的JSON数组之间“拆分”映射值? - How to “split” Map values among different JSON arrays? 如何将文本文件拆分为两个不同的数组? - How Am I able to split a text file into two different arrays? 如何根据元素将一串重复元素和不同值的部分排序到不同的 arrays 中? - How can I sort parts of a string of repeating elements and different values into different arrays based on the element? 如何将文件输入拆分为2个不同的数组java - How to split file input into 2 different arrays java Java如何将数组拆分为两个不同的数组,然后在数组中添加一个数字 - Java How to split array into two different arrays, and then add a number to the arrays 我将如何使用 .split 函数将不同的数据类型分成二维数组? - How would I use the .split function to separate different data types into 2D arrays? 如何在Java中组合来自不同数组的元素? - How can I combine elements from different arrays in java? 如何编写一个方法来散布两个不同长度的数组? - How can I write a method to intersperse two arrays of different lengths? 如何在一个循环中遍历多个不同类型的数组? - How can I iterate over multiple arrays of different types in a loops? 如何按特定顺序打印3个不同的字符串数组的元素? - How can I print the elements of 3 different String Arrays in a certain order?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM