New answers tagged

0

You have three options for storage on an Android device: Shared Preferences Shared Preferences is a Key/Value store, intended to store a small amount of settings type data. It supports primitive types like strings and numbers, stored in an XML format. Shared Preferences are available throughout the application, and they persist between sessions. File ...


1

If you deal with potentially large lists of vastly different sizes that grow and shrink a lot and you are worried about memory allocation, maybe it's a better option to use a LinkedList and clear it at the end. If your lists mainly have the same size, just clear the ArrayList. That has the slight performance advantage over creating a new list of not ...


2

An important difference between both approaches: If references to the array are stored somewhere else, then after myArray.clear everyone holding a reference will now hold a reference to an empty array. After assigning a new array, everyone still holds a reference to the original array.


1

If the myArray variable will go out of scope soon, don't bother with either approach. If you actually need to clear it, set it to null: myArray = null If you are going to reuse the list, clearing it has advantages over creating a new list, because if the list was long once it might likely be long a second time, and clearing it will avoid some internal ...


3

It seems you missed one important difference between myArray.clear() and myArray = new ArrayList<>(): the first one preserves the capacity of the array, thus not freeing the array memory itself. Only the memory of the objects your array elements were referencing to will be freed (as long as the array holded the only reference to those objects). So if ...


0

It sounds like the thing you want to do is this: Pick an array from the weighted list of non-empty arrays Pop a value from that array and add it to the output array Repeat some number of times You seem to be having some trouble doing step 1 in a fair way. My suggestion is to flatten the weighted list into a list containing each array <array-weight> ...


0

If you treat it as a char array, then you don't need a 0 at the end. It's just an array like any other. But if you want to use string functions on it (like the strcpy, strcat in your example), those will look for, rsp. add a null terminator at the end of your arrays, so you will need the reserved space for it! Note that your strcat already writes this 0 ...


1

Use map with key value pairs. Give a try to this: java.util.Map<String,String> contacts = new java.util.HashMap<>(); for (String key : new String[]{"Executive's name", "Executive's address", "Employee's name", "Employee's address"}){ System.out.println("Input the " + key + ": "); contacts.put(key, input.nextLine()); }



Top 50 recent answers are included