I have a list of months with years such as: [12-2014,11-2012,5-2014,8-2012] and I have to sort them with the most recent on top (or the latest date on top) eg. [12-2014,5-2014,11-2012,8-2012] . Does anybody have any idea on how to do this in Java efficiently? EDIT: The class YearMonth is not available, I’m on Java 7 Answer Since you are
Tag: arrays
How do you write a toString() method for an int array for object oriented programming?
How do you write an toString() method for an int array? Say to return the string representation of a 52 card pack? Here is an example array as part of a class: This is being done in an object oriented manner. How would a toString() method be written in this case in order to return a string representation of the
ByteBuffer to int array (each byte)
Related to question byte array to Int Array, however I would like to convert each byte to an int, not each 4-bytes. Is there a better/cleaner way than this: Answer I’d probably prefer
java check if digits in square of a number are not in its cube
Define a positive number to be isolated if none of the digits in its square are in its cube. For example 69 is n isolated number because 163*163 = 26569 and 163*163*163 = 4330747 and the square does …
Java, comparing a Linked List to an Array
Working on a Java program that will read in two different passages, convert both strings to arrays of strings for each word, and then String One to a linked list, which will promptly be sorted into alphabetical order. After the list has been sorted, I get confused. I have a for loop written to get the length of array2 to
Transposing a matrix from a 2D array
I’m self teaching myself some java and I’m stuck on creating a 2D array that initializes it with random values and then creates the transpose of the array. An example output is: Original matrix Transposed matrix ^ Should be the final output. Some help with the code would appreciated! I would like to code to generate error messages if the
How to generate random array of ints using Stream API Java 8?
I am trying to generate random array of integers using new Stream API in Java 8. But I haven’t understood this API clearly yet. So I need help. Here is my code. But this code returns array of objects. What is wrong with it? Answer If you want primitive int values, do not call IntStream::boxed as that produces Integer objects
How to read multiple integer values from one line in Java using BufferedReader object?
I am using BufferedReader class to read inputs in my Java program. I want to read inputs from a user who can enter multiple integer data in single line with space. I want to read all these data in an integer array. Input format- the user first enters how many numbers he/she want to enter And then multiple integer values
The object-oriented approach to a many-to-many relationship
I’m battling at the moment in trying to understand how to approach this issue in an object-oriented way. With a many-to-many relationship such as Students-Subjects, where each student gets a mark for a certain subject, assuming the following: I want to be able to display all the marks for a given student. I want to display all the marks from
Finding closest number to 0
I have an array of integers, and I need to find the one that’s closest to zero (positive integers take priority over negative ones.) Here is the code I have so far: Currently I’m getting a result of -2 but I should be getting 2. What am I doing wrong? Answer Sort the array (add one line of code) so