我有三个列表,即list1,list2,list3我的代码在获取以下列表输出时工作正常: O / P
Getting the list1 objects [man, animal, jackel]
Getting the list2 objects [1, 2, 3]
Getting the list3 objects [bobby, lion, dilip]
现在我想把list4作为[man,1,bobby]
并将list5列为[man,1,bobby , animal,2,lion , jackel,3,dilip]
这是我的代码,它给出了list1,list2,list3
的输出List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
List<String> list3 = new ArrayList<String>();
String splitstring="man,animal,jackel";
String splitid="1,2,3";
String splitscreenname="bobby,lion,dilip";
String[] arrsplitstring=splitstring.split(",");
String[] arrssplitid=splitid.split(",");
String[] arrsplitedscreenname=splitscreenname.split(",");
List<String> wordList = Arrays.asList(arrsplitstring);
List<String> wordid = Arrays.asList(arrssplitid);
List<String> wordscreenlist = Arrays.asList(arrsplitedscreenname);
for (int i=0;i<wordList.size();i++){
list1.add(wordList.get(i));
}
System.out.println("Getting the list1 objects " +list1 );
for(int i=0;i<wordid.size();i++){
list2.add(wordid.get(i));
}
System.out.println("Getting the list2 objects " +list2);
for (int i=0;i<wordscreenlist.size();i++){
list3.add(wordscreenlist.get(i));
}
System.out.println("Getting the list3 objects " +list3);
我通过迭代每个列表对象来尝试它,但我几乎找不到解决方案。
提前致谢
答案 0 :(得分:2)
您要做的是迭代其中一个List
并将所有3个Lists
中的项目添加到新List
。
您可能想要实际做的是拥有一个Animal
对象,包含来自3个原始List
的属性。
这是一个例子(注意:这里是Java 7语法):
// initializing lists
List<String> animals = new ArrayList<>(
Arrays.asList(new String[]{"man", "animal", "jackal"})
);
List<Number> numbers = new ArrayList<>(
Arrays.asList(new Number[]{1, 2, 3})
);
List<String> names = new ArrayList<>(
Arrays.asList(new String[]{"bobby", "lion", "dilip"})
);
// sequential list of objects
List<Object> allObjects = new ArrayList<>();
// class describing an "animal", wrapping the 3 properties
class Animal {
String type;
Number number;
String name;
Animal(String type, Number number, String name) {
this.type = type;
this.number = number;
this.name = name;
}
// fancy String representation
@Override
public String toString() {
return String.format("Type: %s, Number: %d, Name: %s%n", type, number, name);
}
}
// list of Animals
List<Animal> allAnimals = new ArrayList<>();
// iterating the first list (arbitrary choice)
for (int i = 0; i < animals.size(); i++) {
// adding sequentially to objects list
allObjects.add(animals.get(i));
// TODO check for index out of bounds
allObjects.add(numbers.get(i));
allObjects.add(names.get(i));
// adding to animals list sequentially with 3 props each animal
allAnimals.add(new Animal(animals.get(i), numbers.get(i), names.get(i)));
}
// printing out values
System.out.println("All objects...");
System.out.println(allObjects);
System.out.println("All animals...");
System.out.println(allAnimals);
<强>输出强>
All objects...
[man, 1, bobby, animal, 2, lion, jackal, 3, dilip]
All animals...
[Type: man, Number: 1, Name: bobby
, Type: animal, Number: 2, Name: lion
, Type: jackal, Number: 3, Name: dilip
]
答案 1 :(得分:1)
第一个可能是这样的:
List<String> list4 = new ArrayList<String>();
// Check the three lists have the same number of elements.
// If not, return (you have to show and error)
if (! (
(wordListIt.size() == wordidIt.size() )
&&
(wordidIt.size() == wordscreenlistIt.size() )
) ) return;
Iterator<String> wordListIt = wordList.iterator();
Iterator<String> wordidIt = wordid.iterator();
Iterator<String> wordscreenlistIt = wordscreenlist.iterator();
while (wordListIt.hasNext()) {
list4.add(wordListIt.next());
list4.add(wordidIt.next());
list4.add(wordscreenlistIt.next());
break; // exit the while, because you only want the first element.
}
第二个可能是这样的:
List<String> list5 = new ArrayList<String>();
// Check the three lists have the same number of elements.
// If not, return (you have to show and error)
if (! (
(wordListIt.size() == wordidIt.size() )
&&
(wordidIt.size() == wordscreenlistIt.size() )
) ) return;
Iterator<String> wordListIt = wordList.iterator();
Iterator<String> wordidIt = wordid.iterator();
Iterator<String> wordscreenlistIt = wordscreenlist.iterator();
while (wordListIt.hasNext()) {
list5.add(wordListIt.next());
list5.add(wordidIt.next());
list5.add(wordscreenlistIt.next());
}
答案 2 :(得分:0)
您需要遍历每个列表的元素以获得所需的结果。
List<String> list4 = new ArrayList<String>();
list4.add(list1.get(0));
list4.add(list2.get(0));
list4.add(list3.get(0));
List<String> list5 = new ArrayList<String>();
for(int ctr=0;ctr<list3.size();ctr++)
{
list5.add(list1.get(ctr));
list5.add(list2.get(ctr));
list5.add(list3.get(ctr));
}