简体   繁体   English

如何打印数组的三种不同类型的对象(可以存储任何类型的对象)并将其打印在单独的数组中?

[英]How to print three different types of object of an array (in which any type of object is possible to store) and print it in separates arrays?

I have the following codes and I want to modify it so that I can declare three arrays of the same type as objects that I have stored in the ArrayList (for instance arrays of types Integer , Boolean , Double ), scan the ArrayList and each found object to its compatible array. 我有以下代码,我想对其进行修改,以便可以声明与我存储在ArrayList中的对象相同类型的三个数组(例如,类型为IntegerBooleanDoubleArrayList ),扫描ArrayList并找到每个对其兼容数组的对象。 How to do that? 怎么做?

Please help me. 请帮我。 Thank you in advance. 先感谢您。

package task2;

import java.io.ObjectInputStream;
import java.util.ArrayList;

public class Task2 {

    public static void main(String[] args) {
        ArrayList<Object> something=new ArrayList<Object>();

        //Array elements for integer type
        something.add(10);
        something.add(20);
        something.add(30);
        something.add(40);
        something.add(50);
        something.add(60);

        ////Array elements for boolean type
        something.add("The color of the sky is blue");
        something.add("The color of tree is green");
        something.add("The color of blood is red");
        something.add("The color of termeric is yello ");
        something.add("The color of dark is black");
        something.add("The color of watar is unknown ");

        ////Array elements for double type
        something.add("-10");
        something.add("-20");
        something.add("-30");
        something.add("-40");
        something.add("-50");
        something.add("-60");
        something.add("-70");
        something.add("-80");


        // Type code to print the integer type object
        System.out.println("Your array is with integer object:");

        for(int intcounter=0; intcounter<5;intcounter++)
        {
            System.out.println(something.get(intcounter));
        }

        // Type code to print the boolean type object
        {

        }

        // Type code to print the boolean type object
        {

        }
    }
}

I think that this is what you want: 我认为这就是您想要的:

List integers = new ArrayList<Integer>();
List doubles = new ArrayList<Double>();
List booleans = new ArrayList<Boolean>();

for(Object obj: something){
    if(obj instanceof Integer){
       integers.add(obj)
    }
    else if(obj instanceof Double){
       doubles.add(obj)
    }
    else if(obj instanceof Boolean){
       booleans.add(obj)
    }

}

您可以使用Guava ,例如:

Iterable<Integer> onlyIntegers = Iterables.filter(something, Integer.class);

You can do as follows.. But you need to slightly alter your array inputs.. Because the way you do it, you are adding strings as booleans and doubles, you need to add the actualy primitive type. 您可以执行以下操作。但是,您需要稍稍更改数组输入。由于执行此操作的方式是将字符串添加为布尔值和双精度字,因此需要添加实际的基本类型。

    if (obj instanceof Integer) {
         System.out.println("Found Integer:" + obj.toString());
    }
    if (obj instanceof Boolean) {
         System.out.println("Found Boolean:" + obj.toString());
    }
    if (obj instanceof Double) {
         System.out.println("Found Double:" + obj.toString());
    }

I did a solution from your example, hope it makes sense. 我从您的示例中得出了一个解决方案,希望它有意义。 As you can see the types added for boolean and double are the actual type. 如您所见,为boolean和double添加的类型是实际类型。

package task2;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class Task2 {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    ArrayList<Object> something=new ArrayList<Object>();

    //Array elements for integer type
    something.add(10);
    something.add(20);
    something.add(30);
    something.add(40);
    something.add(50);
    something.add(60);

    ////Array elements for boolean type
    something.add(true);
    something.add(false);

    ////Array elements for double type
    something.add(40.1);
    something.add(50.1);
    something.add(60.1);
    something.add(70.1);
    something.add(80.1);


    // Type code to print the integer type object
    System.out.println("Your array is with integer object:");

    for(int i=0; i<something.size();i++){
       Object obj = something.get(i);

       if (obj instanceof Integer) {
          System.out.println("Found Integer:" + obj.toString());
       }
       if (obj instanceof Boolean) {
          System.out.println("Found Bool:" + obj.toString());
       }
       if (obj instanceof Double) {
           System.out.println("Found Double:" + obj.toString());
       }
    }
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM