2017-02-22 66 views
-1

我有兩個的ArrayList合併兩個數組列表

ArrayList中DNAME有值:

mark, 22 

peter, 34 

ken, 55 

ArrayList中DEST有值:

mark, London 

peter, Bristol 

mark, Cambridge 

我想加入合併他們,使他們輸出給出:

  • 馬克

    倫敦

    劍橋

  • 彼得

    布裏斯托爾

這是代碼我有現在的我,不重新盟友usre如何分割的逗號和搜索其他陣列

public class Sample { 

BufferedReader br; 
BufferedReader br2; 

public Sample() { 


    ArrayList<String> dName = new ArrayList<String>(); 
    ArrayList<String> dest = new ArrayList<String>(); 

    String line = null; 
    String lines = null; 

    try { 

     br = new BufferedReader(new FileReader("taxi_details.txt")); 
     br2 = new BufferedReader(new FileReader("2017_journeys.txt")); 

     while ((line = br.readLine()) != null && 
     (lines = br2.readLine()) != null){ 

      String name [] = line.split(";"); 
      String destination [] = lines.split(","); 

      // add values to ArrayList 
      dName.add(line); 
      dest.add(lines); 


      // iterate through destination 
      for (String str : destination) { 
      } 
     } 
    } 
    catch (FileNotFoundException ex) { 
     ex.printStackTrace(); 

     } catch (IOException ex) { 
      ex.printStackTrace(); 

     } finally { 
      try { 
       if (br != null) 
        br.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
} 

public static void main(String[] args) throws IOException { 
    }  
} 
+1

你把它稱爲一個「ArrayList中,」所以我猜這是Java,但你真的應該標記您正在使用什麼語言。另外,我建議看看如何使用markdown格式化您的問題。 Stackoverflow給你一些提示和預覽。很難確切地說出你在問什麼。你能發佈你的代碼嗎?最後,你有Google嗎? – TheCrzyMan

+0

謝謝。是的,我已經嘗試谷歌,但我沒有得到我想要的。很多人都會用錯誤的語言? – diamondgirl

+1

輸出是否爲[[「mark,London,Cambridge」,「peter,Bristol」,「Ken」]''。或者,我想更準確地說,你處理的是什麼類型?字符串?同樣,我會建議看看數據結構「地圖」。這聽起來像它可以幫助你在這裏 – TheCrzyMan

回答

0

現在找到這樣的項目,我不知道這是否是正確的方法,但至少這是工作。

taxi_details.txt

mark, 22 
peter, 34 
ken, 55 

2017_journeys.txt

mark, London 
peter, Bristol 
mark, Cambridge 

的FileReader

import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.util.List; 
import java.util.stream.Collectors; 

public class FileReader { 
    public List<String> read(String fileName) throws IOException{ 
     return Files.lines(new File(fileName).toPath()).collect(Collectors.toList());  
    } 
} 

此類允許你避免了所有凌亂的try-catch塊。

public class Line{ 
    public static final String DELIMITER = ","; 
    public static final int INDEX_NAME = 0; 
    public static final int INDEX_VALUE = 1; 

    private String line; 
    private String[] values; 

    public Line(String line) { 
     this.line = line; 
     this.values = line.split(DELIMITER); 
    } 

    public String getName(){ 
     return this.values[INDEX_NAME]; 
    } 

    public String getValue(){ 
     return this.values[INDEX_VALUE]; 
    } 

    public void emptyValue(){ 
     this.values[INDEX_VALUE] = ""; 
    } 

    @Override 
    public String toString() { 
     return this.line; 
    } 
} 

此類具有準備根據需要用於合併數據的單純prupose。

主要

import java.io.IOException; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
import java.util.Map.Entry; 
import java.util.stream.Collectors; 

public class Main { 
    public static void main(String[] args) throws IOException { 
     FileReader fileReader = new FileReader(); 

     // Read lines 
     List<String> dName = fileReader.read("taxi_details.txt"); 
     List<String> dest = fileReader.read("2017_journeys.txt"); 

     // Convert into proper format 
     List<Line> dNameLines = dName.stream().map(Line::new).collect(Collectors.toList()); 
     List<Line> destLines = dest.stream().map(Line::new).collect(Collectors.toList()); 

     // Remove ID 
     dNameLines.forEach(Line::emptyValue); 

     // Merge lists 
     Map<String, String> joined = join(dNameLines, destLines); 

     // Print 
     for (Entry<String, String> line: joined.entrySet()) { 
      System.out.println(line.getKey() + " --> " + line.getValue()); 
     } 
    } 

    public static Map<String, String> join(List<Line> a, List<Line> b){ 
     Map<String, String> joined = new HashMap<>(); 

     // Put first list into map, as there is no danger of overwriting existing values 
     a.forEach(line -> { 
      joined.put(line.getName(), line.getValue()); 
     }); 

     // Put second list into map, but check for existing keys 
     b.forEach(line -> { 
      String key = line.getName(); 

      if(joined.containsKey(key)){ // Actual merge 
       String existingValue = joined.get(key); 
       String newValue = line.getValue(); 

       if(!existingValue.isEmpty()){ 
        newValue = existingValue + Line.DELIMITER + newValue; 
       } 

       joined.put(key, newValue); 
      }else{ // Add entry normally 
       joined.put(line.getName(), line.getValue()); 
      } 
     }); 

     return joined; 
    } 
} 

您可能希望將join方法轉移到自己的類。

輸出

peter --> Bristol 
ken --> 
mark --> London, Cambridge 
-1

您應該陣列B.

迭代對於每個字符串,分裂的逗號和一個搜索字符串開始分割的第一部分。

然後,將分離的第二部分追加到A.

+0

但是B中沒有出現的A的元素怎麼樣(在本例中爲'ken') –

+0

它會保持原樣,就像上面問題中的輸出一樣。有了這個答案,數組A實際上被修改爲給出所需的輸出。 –