现在有个数组A(s1,s2,s3,s4),S1,S2,S3,S4的值不是true就是false, 怎么把他们的值为true的放入数组B里面。 
public String[] getB(String[] A){   return B; 
}

解决方案 »

  1.   

    boolean 是基础变量
    所以只需要循环判断 A 中 true 的数量,比如 123
    然后 B = new boolean[123];
    在然后循环将 B 的所有元素设置为 true 就行了
      

  2.   


    public Object[] getB(boolean[] A){
       List list = new ArrayList();
       for(boolean temp : A){
         if(temp){
            list.add(temp);
          }
      } 
      return list.toArray();
    }
      

  3.   

    public String[] getB(String[] A){ 
      ArrayList list = new ArrayList();
      for(int i=0;i<a.length;i++){
         if("true".equals[i]){
             list.add("true");
         }
      }
      String[] B = new String[list.size()];
      B=(String[])list.toArray();
      return B; 
    }
      

  4.   


    import java.util.ArrayList;
    import java.util.List;public class StringArrayTest {
    public static String[] getB(String[] a) {
    List<String> list = new ArrayList<String>();
    for (int i = 0; i < a.length; i++) {
    if ("true".equalsIgnoreCase(a[i])) {
    list.add(a[i]);
    }
    }
    String[] b = new String[list.size()];
    return list.toArray(b);
    } public static void main(String[] args) {
    String[] a = { "false", "true", "false", "false" };
    String[] b = getB(a);
    for (int i = 0; i < b.length; i++) {
    System.out.println(b[i]);
    }
    }
    }
      

  5.   

    楼上的有一个小问题就是他传来的参数是String[] 而不是boolean[]
    小修改为public Object[] getB(String[] A){
       List list = new ArrayList();
       for(String temp : A){
         if(Boolean.valueOf(temp)){
            list.add(temp);
          }
      } 
      return list.toArray();
    }
      

  6.   

    public static boolean[] getB(boolean[] a) {        boolean[] b = null;
            int count = 0;
            for (boolean bool : a) {
                if (bool) {
                    count++;
                }
            }
            b = new boolean[count];
            Arrays.fill(b, true);        return b;
        }
      

  7.   

    这个应该先算出有几个等于true的,然后定义一个定长的数组吧,反正数组里都是true,用List时间、空间都不好。