2016-02-19 89 views
0

我有三個列表,即,列表1,列表2,項目list3我的代碼工作中得到以下表輸出的罰款: O/P如何通過在Java中迭代3個不同的列表對象將元素添加到列表對象?

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]

這裏是我的代碼,這給列表1,列表2的輸出,項目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); 

我被每個列表的迭代嘗試它對象,但我幾乎找不到解決方案。

在此先感謝

+1

最佳的解決辦法是有一個'Object'將包含所有三個屬性,只有一個'List'這隻會包含這個「Object」的類型。 – SomeJavaGuy

回答

2

你想要做什麼List S的一個迭代,並添加來自所有3 Lists項目到新的List

你可能想實際做的是有一個Animal對象的種類,包含來自3個原始List s的屬性。

下面是一個例子(注意:這裏的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 
] 
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)); 
} 
1

第一個可能是像t他:

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()); 
} 
相關問題