I reuse the same ArrayList in a for loop, and I use
for loop results = new ArrayList<Integer>(); experts = new ArrayList<Integer>(); output = new ArrayList<String>(); ....
to create new ones.
I guess this is wrong because I allocate new memory. It's right? If so, how can I delete them?
Added: another example
I create new variables every time I call this method. Is this a good practice? I want to create a new precision matching the found .. etc? Or should I declare them in my class, do not allocate more and more memory outside the method?
public static void computeMAP(ArrayList<Integer> results, ArrayList<Integer> experts) { //compute MAP double precision = 0; int relevantFound = 0; double sumprecision = 0;
thank
ArrayList.clear() ; , - "", Java , . , ( ), clear .
ArrayList.clear()
clear
, ; ( , ), . (.. new), .
new
:, , . , new "" ; - - , , , , - ., - ; , , , , - .
-, java, .
, . 100k, , 3 . List.clear().
, . , , 256 64 .
, , . . , Android for (int = 0...) (Object o: someList). , . , clear() .
, clear() .
clear()
, . , .
, 100 000 . . .
:
Elapsed time - in the loop: 2198 Elapsed time - with clear(): 1621 Elapsed time - in the loop: 2291 Elapsed time - with clear(): 1621 Elapsed time - in the loop: 2182 Elapsed time - with clear(): 1605
, , . , , .
: Java 1.6.0_19, Centrino 2 Windows. , .
import java.util.*; public class Main { public static void main(String[] args) { // Allocates a new list inside the loop long startTime = System.currentTimeMillis(); for( int i = 0; i < 100000; i++ ) { List<String> l1 = new ArrayList<String>(); for( int j = 0; j < 1000; j++ ) l1.add( "test" ); } System.out.println( "Elapsed time - in the loop: " + (System.currentTimeMillis() - startTime) ); // Reuse the list startTime = System.currentTimeMillis(); List<String> l2 = new ArrayList<String>(); for( int i = 0; i < 100000; i++ ) { l2.clear(); for( int j = 0; j < 1000; j++ ) l2.add( "test" ); } System.out.println( "Elapsed time - with clear(): " + (System.currentTimeMillis() - startTime) ); } }
ArrayLists 5 . , 4 ( , , 8 ). int "" , 24 . 16 , ( ), 40 ArrayList(). , , .
, , Java 1.6.16, JVM ( ?), "" , . "" , .
, , - , . , ArrayList ArrayList.clear() .
, , , "" "" . , - (.. ). (. ).
, , , , . , , .
Source: https://habr.com/ru/post/1744697/More articles:ForEach with EditorFor - c #Difference between C ++ keyboard code and JAVA KeyEvent keyboard key - javaEntity Framework 4.0 - ΡΡΡΠ»ΠΊΠ° ΡΠΎΠ»ΡΠΊΠΎ Π΄Π»Ρ ΠΊΠΎΠ΄Π° - entity-framework-4ASP.NET 4.0 Web Form Routing - c #iTunes Track Location Software Update - applescriptHow to extract keywords from an HTML page in C #? - c #ΠΡΠΆΠ½Π° ΠΏΠΎΠΌΠΎΡΡ Π² ΠΏΠΎΠ½ΠΈΠΌΠ°Π½ΠΈΠΈ Π²ΡΠ²ΠΎΠ΄Π° kcachedgrind - valgrindΠΡΠΈΠ±ΠΊΠ° Nhibernate "ΠΎΠ΄ΠΈΠ½ ΠΊ ΠΎΠ΄Π½ΠΎΠΌΡ" Ρ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ Π²Π²ΠΎΠ΄Π° Π΄ΠΎΡΠ΅ΡΠ½Π΅Π³ΠΎ ΠΎΠ±ΡΠ΅ΠΊΡΠ° - nhibernateΠ Π΅Π°Π»ΠΈΠ·Π°ΡΠΈΡ BindingList - c#Android: No specific array resource found? - androidAll Articles