2013-12-19 57 views
0

我需要幫助將數據從4 ArrayLists傳輸到多維數組。請查看我的變量中的註釋。將數據從多個數組列表轉換爲多維數組

我需要把從所有的ArrayList數據到card[][]格式爲:

card[][] = {{questions,answers,category,essay}}; 

代碼:

import java.io.BufferedReader; 
import java.io.FileReader; 
import java.util.ArrayList; 
import java.util.List; 

import javax.swing.JFileChooser; 

public class Test { 
    static JFileChooser chooser = new JFileChooser(); 

    //these need to go into card[][] 
    static List<String> questions = new ArrayList<String>(); 
    static List<String> answers = new ArrayList<String>(); 
    static List<String> categories = new ArrayList<String>(); 
    static List<String> essays = new ArrayList<String>(); 


    static String[][] card; 

    public static void main(String[] args) { 

     OpenFile(); 

    } 

    public static void OpenFile() { 
     int retrival = chooser.showOpenDialog(null); 
     if (retrival == JFileChooser.APPROVE_OPTION) { 
      String line; 

      try (BufferedReader br = new BufferedReader(new FileReader(
        chooser.getSelectedFile()))) { 
       while ((line = br.readLine()) != null) { 
        String[] splitted = line.split("Question: "); 
        String[] splittedAnswer = line.split("Answer: "); 
        String[] splittedCategory = line.split("Category: "); 
        String[] splittedEssay = line 
          .split("Essay Question Possibility: "); 

        if (splittedAnswer.length == 2) { 
         answers.add(splittedAnswer[1]); 
        } else if (splitted.length == 2) { 
         questions.add(splitted[1]); 

        } else if (splittedCategory.length == 2) { 
         categories.add(splittedCategory[1]); 

        } else if (splittedEssay.length == 2) { 
         essays.add(splittedEssay[1]); 

        } 

       } 
       br.close(); 

      } catch (Exception ex) { 
       ex.printStackTrace(); 
      } 
     } 
    } 
} 
+0

哇......你想在這裏完成什麼?爲什麼你不能僅僅使用一些簡單的東西,比如一個包含問題,答案,類別,散文的類的數組列表? – futureelite7

回答

0

試試這個,

  String card[][] ; 
      List<String> questions = new ArrayList<String>(); 
      List<String> answers = new ArrayList<String>(); 
      List<String> categories = new ArrayList<String>(); 
      List<String> essays = new ArrayList<String>(); 
      questions.add("11"); 
      questions.add("12"); 
      questions.add("13"); 

      answers.add("21"); 
      answers.add("22"); 
      answers.add("23"); 

      categories.add("31"); 
      categories.add("32"); 
      categories.add("33"); 

      essays.add("41"); 
      essays.add("42"); 
      essays.add("43"); 

      card=new String [essays.size()][4]; 
      for(int i=0;i<questions.size();i++) 
      { 
       card[i][0]=questions.get(i); 
       card[i][1]=answers.get(i); 
       card[i][2]=categories.get(i); 
       card[i][3]=essays.get(i); 
      } 

輸出將被

{{「11」,「21」,「31」,「41」},{「12」,「22」,「32」,「42」},{「13」,「23」,「33 「,」43「},...};

0

我認爲所有的數組列表都有一個大小。如果 '不',你應該首先找到的ArrayList的最小尺寸,然後用它來代替questions.size():

card = new String[questions.size()][4]; 

for (int i = 0; i < card.length; i++){ 
    card[i][0] = questions.get(i); 
    card[i][1] = answers.get(i); 
    card[i][2] = categories.get(i); 
    card[i][3] = essays.get(i); 
} 

結果會是這樣:

{{ 「Q1」, 「A1」, 「c1」,「e1」,{「q2」,「a2」,「c2」,「e2」},{「q3」,「a3」,「c3」,「e3」},{「q4」 「a4」,「c4」,「e4」},...};

1

假設所有的數組列表,你可以按照下面的做相同的大小,

String[][] card = new String[4][questions.size()]; 
for (int i = 0; i < questions.size(); i++) { 
    card[0][i] = questions.get(i); 
    card[1][i] = answers.get(i); 
    card[2][i] = categories.get(i); 
    card[3][i] = essays.get(i); 
} 

建議

但我建議你用面向對象的數據結構,牽着你的數據。不建議使用多列表或二維數組。

您可以簡單地定義你的結構

public class Card { 
    String question; 
    String answer; 
    String categories; 
    String essay; 
} 

而且你必須有列表作爲。

List<Card> cards = new ArrayList<Cards>(); 
+0

我同意這個建議,如果你用面向對象的語言寫作,爲什麼不利用它提供的組織優勢 – DeathByTensors