<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[JavaRanch: Latest posts for the topic "Array vs arraylist"]]></title>
		<link>http://www.coderanch.com/forums/t/15/Performance/Array-vs-arraylist</link>
		<description><![CDATA[Latest messages posted in the topic "Array vs arraylist"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Array vs arraylist</title>
				<description><![CDATA[I had a 7mb file. I read the file, parsed its records, made an object out every line and stored in arraylist. It threw memory exceeded exception. <br /> I used an array of 60000 size and read all the lines, parsed and stored in the array. <br /> The process completed in 1 sec.<br />  <br /> Why is there such a large difference in performance between these two?]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/202947/983843</guid>
				<link>http://www.coderanch.com/forums/posts/preList/202947/983843</link>
				<pubDate><![CDATA[Mon, Aug 20 2007 02:47:00 MDT]]></pubDate>
				<author><![CDATA[fahad siddiqui]]></author>
			</item>
			<item>
				<title>Array vs arraylist</title>
				<description><![CDATA[That's not really a difference in performance, is it? One worked, and the other didn't!<br />  <br /> If you do not pre-size your <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" class="api" title="Java API" target="_new" rel="nofollow">ArrayList</a>, it starts off with tiny internal array, and re-sizes the array each time it becomes full. To do that, it creates a bigger array (<a href="http://www.javaranch.com" class="faq" title="A Friendly Place for Java Greenhorns" target="_new">Java</a> arrays cannot actually be resized), and copies the old contents into it, before discarding the old array. So it briefly has to have enough memory for the old and new arrays. Also, the new array is necessarily bigger than the current size.<br />  <br /> You can make <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" class="api" title="Java API" target="_new" rel="nofollow">ArrayList</a> more memory-efficient and faster by pre-sizing it. That is, specifying an appropriate size when constructing the <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" class="api" title="Java API" target="_new" rel="nofollow">ArrayList</a>. Presumably, if 60000 was enough for your array-based solution, the same size is suitable for the <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" class="api" title="Java API" target="_new" rel="nofollow">ArrayList</a> solution.<br />  <br /> But do you really know in advance how many items there are? If you do, then an array or pre-sized <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" class="api" title="Java API" target="_new" rel="nofollow">ArrayList</a> are good solutions. If you do not, then your array-based "solution" is not practical, as you cannot choose an appropriate array size. It's for exactly that type of situation that <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" class="api" title="Java API" target="_new" rel="nofollow">ArrayList</a> was created. (Collections have some other advantages, too).<br />  <br /> If you need to use <a href="http://java.sun.com/javase/6/docs/api/java/util/ArrayList.html" class="api" title="Java API" target="_new" rel="nofollow">ArrayList</a>, then you may need to increase the maximum heap setting of your JVM. Look up options like -Xmx on the "java[.exe]" command.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/202947/983844</guid>
				<link>http://www.coderanch.com/forums/posts/preList/202947/983844</link>
				<pubDate><![CDATA[Mon, Aug 20 2007 04:24:00 MDT]]></pubDate>
				<author><![CDATA[Peter Chase]]></author>
			</item>
	</channel>
</rss>
