Skip to content

Resize an Array while keeping current elements in Java?

I have searched for a way to resize an array in Java, but I could not find ways of resizing the array while keeping the current elements. I found for example code like int[] newImage = new int[newWidth];, but this deletes the elements stored before. My code would basically do this: whenever a new element is a…

Epoch time confusion, clarification needed

Given: As i understand it, the result comes back with time since the epoch, in milliseconds The current time as UTC milliseconds from the epoch. Given that my test always sets the objects the same, why are results coming up different as time goes by? UPDATE: I continue to second guess myself due to For the sa…

How to search in a List of Java object

I have a List of object and the list is very big. The object is Now I have to search for a specific value of an object in the list. Say if value3==’three’ I have to return those objects (My search is not always based on value3) The list is What is the efficient way of doing it? Thanks. Answer

List to ArrayList conversion issue

I have a following method…which actually takes the list of sentences and splits each sentence into words. Here is it: public List getWords(List strSentences){ allWords = …

Java—how can I dynamically reference an object’s property?

In javascript, I can do this: Can I do anything close in Java? Answer Yes, you can do it by reflection with something along the following lines: However, if you are not very familiar with Java, this approach should be avoided if at all possible, as it is somewhat dangerous and error prone. For instance, there…

Create a List of byte[]

How might you go about creating a List of byte[] (not Byte)? I want something like the following: Answer That will work fine because arrays are objects in Java, so you can build Lists out of them. Note that only in Java 7 can you do In older versions you must restate the byte[]: This has been brought up alrea…