我有以下代码,我想修改它,以便我可以声明三个与我存储在ArrayList
中的对象相同类型的数组(例如类型Integer
的数组, Boolean
,Double
),将ArrayList
和每个找到的对象扫描到其兼容阵列。怎么做?
请帮帮我。提前谢谢。
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
{
}
}
}
答案 0 :(得分:1)
我认为这就是你想要的:
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)
}
}
答案 1 :(得分:0)
您可以使用Guava,例如:
Iterable<Integer> onlyIntegers = Iterables.filter(something, Integer.class);
答案 2 :(得分:0)
您可以执行以下操作..但您需要稍微改变您的数组输入..因为您这样做的方式,您添加字符串作为布尔和双打,您需要添加实际原始类型。
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());
}
我从你的例子中做了一个解决方案,希望它有意义。正如您所看到的,为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());
}
}
}
}