Michael Curley

Greenhorn
+ Follow
since May 21, 2019
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
5
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Michael Curley

Some further info about what way I was approaching the problem. I realised that I was not supplying a list to the Collections.sort method, so I made a copy of the original list and returned this from a new method in Laptop Manager class. However, this did not work either for some reason, and I spent most of last night trying to figure out why. However, I am going to try the approach you have suggested.

Thanks guys
Dave/Piet

Thank you guys so much. That's exactly what I was looking for :-)

Best Regards


Thanks for your reply. I appreciate the feedback.

Thanks for the pointer. I must take a closer look at the Comparator interface, and see if I can work out the method you are talking about.

Best regards

Hello,

I have created a class laptop as below



I have also created a LaptopManager class, the objective is to manage a list of Laptops as below




After doing this, I created a SortComparator class which implements the comparator interface as below




Now, in my runner class I want to sort a list of Laptops stored in the ArrayList in StudentManager......



My question is can this be done in my runner class, knowing I want to keep this list fully encapsulated in the LaptopManager Class......

Any help appreciated......

Liutauras Vilda wrote:Michael, hi.

What the output supposed to be if the matrix were:
A  A  A  A
Z  Y  X  W
X  A  D  F
G  V  X  G

>?



Hi Liutauras,

In that case the matrix should not change. But I think I know what you are getting at, i.e. having a stable sort algorithm.
As the first row is a code-word, it is possible that what you have described could well happen. I have tested my program using "AAAA" as a codeword, and
am happy to report that nothing in the matrix changes.

On the other hand, I know my code could be more efficient, but I am a java newbie, and I am improving all the time, thanks in no small measure to excellent
websites like this.

Mike
4 years ago

Piet Souris wrote:hi Michael,

was on vacation without internet, just got back. Glad we could help.

This is the one-liner that I mentioned, but on second thought the line was way too long, so I split it over three lines. .

Thank you so much for all your help.......
4 years ago

Piet Souris wrote:

Michael Curley wrote: (...) This was my first approach.


hi Michael,

well, it takes just one line of code to get the indices, after sorting the letters of the code word, but that involves some methods that are probably unfamiliar to you at this moment.

So, if you have a codeword with length L, then create an array A of length L, and initialize it with 0, 1, 2, ..., L - 1.
Now, if you sort your codeword and you swap charAt(i) with charAt(j), then also swap A[i] and A[j]. When done, the A array contains the sorted indices. Now, for each int[] row of your 2D array, make sure that element[i] will be equal to element[A[i]]. Be careful not to overwrite elements, though.



Sorry, got sidetracked with other stuff last few days....

Thank you for all your help guys....
4 years ago

Piet Souris wrote:Hmmm...

if it were me, then I would produce a very simple int[] Comparator (that compares on the first elements), and do a sort on the transpose of the matrix.

But this is the Beginners Forum, and I don't think this exercise is easy at all. I would not use terms like 'very easy'. Let's wait for Michael to hear what he thinks.



This sounds interesting, but I am unfamiliar with this approach. I need to find out more information as to how this works.......
4 years ago

Piet Souris wrote:Or, for something completely different: if your first row = "b a d c", and you sort it, then the original indices (0 1 2 3) will now be 1 0 3 2. What does that mean for the other rows?



This was my first approach. So I came up with a method that assigns numbers to letters in the "codeword" from A-Z, and as as you say it converts (0 1 2 3) to (1 0 3 2) in this example. The reason I went down this route is because the program takes in the "codeword" from the user, so the length of each row, although 4x in the example given in my first post, with JAVA as the codeword, could be much larger depending upon which "codeword" the user decides to use. So if, for example, the uses decides to use "Apples" as the codeword, the matrix size will be (6 x "X"), where X will be a function of some other Plaintext input....(and the method will convert(0 1 2  3 4  5) to (0 3 4 2 1 5). I understand that I now need to arrange each row based on these indicies, but I haven't quite figured that bit out yet.

I am also afraid the code I am writing may not be the most efficient, and I AM sure there are much easier approaches to the problem. Any Takers?
4 years ago
Hi Al,

Yeah as was already pointed out above, my statement wasn’t accurate or concise enough. I couldn’t put it better than this.......

“What you actually want to do is, given a matrix (2-dimensional array) of characters, take each column and arrange them by the ascending collating sequence of the character in the top row of that matrix”
4 years ago
Hi Tim,

Thank you for your reply.
Your interpretation was spot on. I understand that a big part of learning how to write good code is being able to describe concisely and accurately what you are trying to do, and I know I am a long way from being able to do that at the moment.
Some good advice, thank you.
4 years ago
Hi Campbell,

Thanks for your reply. I will try and follow your advice.
I'm a total novice, and don't believe I know about the standard swap technique either,
The 2D array is actually the result of arranging a string which has being input by the user. The string is converted to uppercase, and into a char array(one dimensional)....This is then arranged into a 2D char[][] matrix, where the rows and column sizes are determined by a codeword, and the length of the string which has being input.

The next step then, was what I am trying to achieve in my first post.....

Thank you for your advice thus far.
4 years ago
Hi Guys,

New to Java and having some difficulty with an assignment, so any help appreciated.

Basically, I have a 2 dimensional array, which is a simple char matrix. I now need to sort this alphabetically by the first row. In doing this, I need to transpose the columns also.

So for example, my 2 dimensional array ends up as follows

J  A  V  A
F  G V  X
X  A  D  F
G  V  X  G

which I want to arrange  like below,  with each column sorted alphabetically....

A  A  J  V
G  X  F V
A  F  X  D
V  G  G X
4 years ago