【问题标题】:Is there a mistake in adding String[] to ArrayList?将String[] 添加到ArrayList 是否有错误?
【发布时间】:2014-04-10 11:14:22
【问题描述】:

我有以下 XML 文件。如您所知,有 2 个单独的文档:

 <doc>
<str name="id">1</str>
<str name="full_message_t">"""“God loves you more daddy....God loves them more</str>
<str name="source_t">” tweets teenage daughter of #MH370 chief steward http://t.co/Abw96As7xv"""</str>
<str name="news_t">TODAY</str>
<str name="link_t">"""“God loves you more daddy....God loves them more</str>
<long name="_version_">1464787951004155905</long>
</doc>

<doc>
<str name="id">2</str>
<str name="full_message_t">"""Flight #MH370 ended flight in southern Indian Ocean</str>
<str name="source_t"> says @NajibRazak http://t.co/NfesnDrcsP"""</str>
<str name="news_t">TOMORROW</str>
<str name="link_t">"""Flight #MH370 ended flight in southern Indian Ocean</str>
<long name="_version_">1464787951005204483</long>
</doc>

以及下面的 SAXParser 代码:

public class ReadXMLFile {

    public static void main(String[] args) {
        //Declaration
          final String row[] = new String[5];         //id, full_message_t, source_t, news_t, link_t
          final ArrayList<String[]> tableResult = new ArrayList<String[]>();    //Store ArrayList of row[]

          try {

              SAXParserFactory factory = SAXParserFactory.newInstance();
              SAXParser saxParser = factory.newSAXParser();

              DefaultHandler handler = new DefaultHandler() 
              {
                  //When these are TRUE, extract the ocntent
                    boolean b_id = false;
                    boolean b_full_message_t = false;
                    boolean b_source_t = false;
                    boolean b_news_t = false;
                    boolean b_link_t = false;                                       
                    //############################################################################################
                    //Look for <start> tags
                    public void startElement(String uri, String localName,String qName, 
                            Attributes attributes) throws SAXException {

                            System.out.println("Start Element :" + qName);

                            //Only interested in <str>
                            if (qName.equalsIgnoreCase("str")) {
                                String name = attributes.getValue("name");

                                    //Check for name="id"
                                if(name.equalsIgnoreCase("id")){
                                    b_id = true;
                                }                                
                                    //Check for name="full_message_t"
                                else if(name.equalsIgnoreCase("full_message_t")){
                                    b_full_message_t = true;
                                }
                                else if(name.equalsIgnoreCase("source_t")){
                                    b_source_t = true;
                                }
                                else if(name.equalsIgnoreCase("news_t")){
                                    b_news_t= true;
                                }
                                else if(name.equalsIgnoreCase("link_t")){
                                    b_link_t = true;
                                }                             
                            }//end if

                    }//end startElement

                    //############################################################################################
                    //Look for <end> tags
                    public void endElement(String uri, String localName,
                            String qName) throws SAXException {

                            System.out.println("End Element :" + qName);

                            //When reach </doc>, row Array is complete. Add into ArrayList
                            if (qName.equalsIgnoreCase("doc")) {
                                    System.out.println("Push row into Arraylist : " + row[0]);
                                    tableResult.add(row);
                            }
                    }//end endElement                        
                    //############################################################################################
                    //Get the content
                    public void characters(char ch[], int start, int length) throws SAXException {

                            if (b_id) {
                                    //System.out.println("id : " + new String(ch, start, length));
                                    row[0] = new String(ch, start, length);
                                    System.out.println("The id is " + row[0]);
                                    b_id = false;
                            }
                            else if (b_full_message_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[1] = new String(ch, start, length);
                                    System.out.println("The fullmsg is " + row[1]);
                                    b_full_message_t = false;
                            }
                            else if (b_source_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[2] = new String(ch, start, length);
                                    System.out.println("The source is " + row[2]);
                                    b_source_t = false;
                            }
                            else if (b_news_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[3] = new String(ch, start, length);
                                    System.out.println("The news is " + row[3]);
                                    b_news_t = false;
                            }
                            else if (b_link_t) {
                                    //System.out.println("fullmsg : " + new String(ch, start, length));
                                    row[4] = new String(ch, start, length);
                                    System.out.println("The link is " + row[4]);
                                    b_link_t = false;
                            }

                    }//end characters

           };//end DefaultHandler
                               //############################################################################################
              //Read the String  
              //saxParser.parse("solrTest.xml", handler);
              File file = new File("solrTest.xml");
              InputStream inputStream= new FileInputStream(file);
              Reader reader = new InputStreamReader(inputStream,"UTF-8");

              InputSource is = new InputSource(reader);
              is.setEncoding("UTF-8");

              saxParser.parse(is, handler);

           } catch (Exception e) {
             e.printStackTrace();
           }

          //Test output Arraylist
          System.out.println("Test Result");
          System.out.println(tableResult.get(0));
          System.out.println(tableResult.get(1));

    }
}

到执行结束时,应该有 2 个字符串数组,它们将被添加到 ArrayList 中。但是,在我打印出地址的最后一行时,地址是相同的。 换句话说,最后一个文档被存储在ArrayList 两次中。 为什么会这样?第一个文档怎么了?

【问题讨论】:

  • 我只看到你创建了一个字符串数组。

标签: java xml arrays string parsing


【解决方案1】:

你必须在调用后重新初始化你的行数组

tableResult.add(row);
row = new String[5];

否则它总是有相同的引用。这意味着您的列表有两个条目,引用同一个数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-24
    • 1970-01-01
    • 2014-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    相关资源
    最近更新 更多