Skip to content
Advertisement

Tag: arrays

How to sort an array of objects in Java?

My array does not contain any string. But its contains object references. Every object reference returns name, id, author and publisher by toString method. Now I need to sort that array of objects by the name. I know how to sort, but I do not know how to extract the name from the objects and sort them. Answer You have

Java two varargs in one method

Is there any way in Java to create a method, which is expecting two different varargs? I know, with the same object kind it isn’t possible because the compiler doesn’t know where to start or to end. But why it also isn’t possible with two different Object types? For example: Is there any way to create a method like this?

Java: how to have an array of subclass types?

Say I have a super class “Animal” and subclasses “Cat”, Dog, Bird”. Is there a way to have an array of subclass type rather than class instances with which I’ll be able to instantiate instances of each possible subclass? To simplify, I want this: How can I do that? Edit: I don’t want an array of instances of these subclasses,

Can an array be used as a HashMap key?

If a HashMap’s key is a String[] array: Can you access the map by using a newly created String[] array, or does it have to be the same String[] object? Answer It will have to be the same object. A HashMap compares keys using equals() and two arrays in Java are equal only if they are the same object. If

Find substring in List and return Index Number

I have an List, with Strings like: Now i would like to get the Index Number for the Substring “One”. How can i get this? I only could make it if i convert it to an Array and then: Isn’t there a way to search for the Substring without converting it to an Array? Answer List already contains indexes

hasPrevious() Method not working

I don’t understand why my hasPrevious iterator is not functioning, it looks like this: I have another function using hasNext() and it works, but this one will not. I am not sure if this method is no longer in existence or not, but when I look into it, Java websites speak of it. I don’t understand why it is not

Advertisement