Skip to content
Advertisement

Tag: arrays

What is this: [Ljava.lang.Object;?

I get this when I call toString on an object I received from a function call. I know the type of the object is encoded in this string, but I don’t know how to read it. What is this type of encoding called? Answer [Ljava.lang.Object; is the name for Object[].class, the java.lang.Class representing the class of array of Object. The

Is there an equivalent to memcpy() in Java?

I have a byte[] and would like to copy it into another byte[]. Maybe I am showing my simple ‘C’ background here, but is there an equivalent to memcpy() on byte arrays in Java? Answer You might try System.arraycopy or make use of array functions in the Arrays class like java.util.Arrays.copyOf. Both should give you native performance under the hood.

Java : convert List of Bytes to array of bytes

Trying to solve what should be a simple problem. Got a list of Bytes, want to convert it at the end of a function to an array of bytes. compiler doesn’t like syntax on my toArray. How to fix this? Answer The compiler doesn’t like it, because byte[] isn’t Byte[]. What you can do is use commons-lang’s ArrayUtils.toPrimitive(wrapperCollection): If you

How to add new elements to an array?

I have the following code: Those two appends are not compiling. How would that work correctly? Answer The size of an array can’t be modified. If you want a bigger array you have to instantiate a new one. A better solution would be to use an ArrayList which can grow as you need it. The method ArrayList.toArray( T[] a )

Creating a New Reverse Java Array

CodingBat > Java > Array-1 > reverse3: Given an array of ints length 3, return a new array with the elements in reverse order, so {1, 2, 3} becomes {3, 2, 1}. I can’t get this to work properly, usually the last int in the array, becomes every single int in the new array Answer You don’t want a two-level

What is more efficient: System.arraycopy or Arrays.copyOf?

The toArray method in ArrayList, Bloch uses both System.arraycopy and Arrays.copyOf to copy an array. How can I compare these two copy methods and when should I use which? Answer The difference is that Arrays.copyOf does not only copy elements, it also creates a new array. System.arraycopy copies into an existing array. Here is the source for Arrays.copyOf, as you

Java Array Sort descending?

Is there any EASY way to sort an array in descending order like how they have a sort in ascending order in the Arrays class? Or do I have to stop being lazy and do this myself :[ Answer You could use this to sort all kind of Objects Arrays.sort() cannot be used directly to sort primitive arrays in descending

How to map a PostgreSQL array with Hibernate

Has anyone successfully mapped a numeric array in PostgreSQL to a numeric array in Java via Hibernate? SQL: Mapping: Class: I get an exception when querying the table. Answer Hibernate does not support database arrays (e.g. ones mapped to java.sql.Array) out of the box. array and primitive-array types provided by Hibernate are for mapping Java arrays into backing table –

How to find the index of an element in an array in Java?

I am looking to find the index of a given element, knowing its contents, in Java. I tried the following example, which does not work: Can anyone please explain what is wrong with this and what I need to do to fix it? Answer In this case, you could create e new String from your array of chars and then

Advertisement