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
Tag: arrays
Print out elements from an Array with a Comma between the elements
I am printing out elements from an ArrayList and want to have a comma after each word except the last word. Right now, I am doing it like this: It prints out the words like this: The problem is the last comma. How do I solve it? Answer Print the first word on its own if it exists. Then print
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,
Java GC overhead limit exceeded – Custom solution needed
I am evaluating different data from a textfile in a rather large algorithm. If the text file contains more than datapoints (the minimum I need is sth. like 1.3 million datapoints) it gives the following error: When I’m running it in Eclipse with the following settings for the installed jre6 (standard VM): Note that it works fine if I only
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
Multiplying two matrices in Java
I am currently developing a class to represent matrices, it represents any general mxn matrix. I have worked out addition and scalar multiplication but I am struggling to develop the multiplication of two matrices. The data of the matrix is held in a 2D array of doubles. The method looks a little bit like this: It will return the product
Making a concat operator to add char array to a string. Printing a reference variable of a string object
So far, if I try to use the concat operator I get this error Tester.java:14: cannot find symbol symbol : method concat(MyString) location: class MyString System.out.println(hello.concat(goodbye));…