Import array to arraylist except null elements?
-
Another java newbie question. How can I import an array into an arraylist except null values? I find array to be unyielding when it comes to moving around and removing elements and reducing/increasing the size of the array. I need to use array during the first part of the program for indexing purposes and export the contents to an arraylist except null elements. The latter part of my program needs iteration using the new arraysize trimmed to actual number of elements where trimtosize is very helpful. The actual problem I have with using arraylist at the very beginning is that when I set an element value to null, it affects the values of succeeding elements. For example i have an arraylist of 1, 2, 3, 4, 5. If I set the value of index 2 to null, the output becomes 1,2, null, 3, 4, so now I'm missing 5. It's easier to remove it instead of setting it to null but the index of the elements are crucial... If you can suggest a way I can set element values to null without having the other elements affected it would be better.. Many thanks in advance.
-
Answer:
I don't think that's possible, although I could see how you might think that's possible, since an array is an object. The collections classes are useful because they have that functionality built in. You can use Arrays.toList() to transform an array into a list. I think List.add() will return false, for null data. It seems like you want to add a null value at an index. Have you tried using List.add(T value, int index)?
Myth at Yahoo! Answers Visit the source
Related Q & A:
- How to post an array of complex objects (that has an array of complex objects) with jQuery?Best solution by Stack Overflow
- How do you create a Two-Dimensional ArrayList?Best solution by Stack Overflow
- how can i remove an array from an array?Best solution by Stack Overflow
- How to perform operation on Arraylist?Best solution by Stack Overflow
- How to save new objects into an arrayList?Best solution by stackoverflow.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.