简体   繁体   中英

Java system.out not printing all occurances

I am currently parsing an XML document and storing the XML data in a String[]. This is what I do:

db = dbf.newDocumentBuilder();
Document dom = db.parse(in);
Element docElem = dom.getDocumentElement();
NodeList valuesList= docElem.getElementsByTagName("values");

Element value = (Element) valuesList.item(0);

//Split on every white space
String[] myValues = value.getFirstChild().getNodeValue().split("\\s+");

System.out.println(myValues.length);
//Output: 5400 

for(String val : myValues){
    System.out.println("Value: " + val);
    //This prints out random amount of lines in the console, see figure.
}

The output from the loop varies. See the picture below.

系统输出

The result could be 4145 the first execution and 4198 the second time.

Shouldn't this output be 5400 lines since the String[] holds 5400 items? Why is it not?

Thankful for any kind of explanation.

Marcus

System.out goes to logcat and the logcat is just a buffer with limited capacity. When the capacity is exceeded, older entries are thrown out. The proportion of your System.out messages in the buffer depends on other logcat activity in the system.

See What is the size limit for Logcat and how to change its capacity?

If Logcat size limit is not your problem then try the following as well -

    //Split on every white space
    String[] myValues = value.getFirstChild().getNodeValue().split("\\s+");

    System.out.println(myValues.length);
    //Output: 5400 

    List<String> stringList = Arrays.asList(myValues);

    List<String> list = Collections.synchronizedList(stringList);

    synchronized(list) 
    {
       Iterator<String> iterator = list.iterator(); 

       while (iterator.hasNext())
          System.out.println(iterator.next());
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM