| Author |
Idea to sort a list and get a result
|
Aditya Sirohi
Ranch Hand
Joined: Jan 05, 2010
Posts: 91
|
|
Hola Friends,
I have a question i have an output from a method like below:-
CINSTALLDIR CM
CINSTALLDIR CT
CINSTALLDIR PJ
CINSTALLDIR RM
CINSTALLDIR RX
CINSTALLDIR SV
CINSTALLDIR TM
I am trying to write a method that take the above as input and generates this:-
CINSTALLDIR CM CT PJ RM RX SV TM
If i try to put them into an array, i think i will be difficult, should i try using Hash Map. Any other data structure you suggest?
Thanks
Aditya
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
|
Take the first entry from the first column and follow it by all entries from the second column. You don't need any collections at all.
|
 |
Aditya Sirohi
Ranch Hand
Joined: Jan 05, 2010
Posts: 91
|
|
The input are in an array. So i wrote the code like this as below. Does not work!!
-Aditya
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16689
|
|
Aditya Sirohi wrote:The input are in an array. So i wrote the code like this as below. Does not work!!
So, fix it. It looks pretty close. Change a println() to a print(). Add a condition on the prints. etc. And you'll be done.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Aditya Sirohi
Ranch Hand
Joined: Jan 05, 2010
Posts: 91
|
|
Thanks Henry,
I am working on the idea you gave me. I never know print() works this way. This was a good exposure.
Thanks
Aditya
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32665
|
|
|
But don't call the String[] Regex. That will confuse anybody who reads your code quickly.
|
 |
Aditya Sirohi
Ranch Hand
Joined: Jan 05, 2010
Posts: 91
|
|
Not able to do it, still trying
|
 |
Aditya Sirohi
Ranch Hand
Joined: Jan 05, 2010
Posts: 91
|
|
Hello Cambell,
I use String[] Regex because that contains Split strings that are in test array. So then i have two fields.
Thanks
Aditya
|
 |
Aditya Sirohi
Ranch Hand
Joined: Jan 05, 2010
Posts: 91
|
|
Idea struck and i got it
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32665
|
|
|
But calling your String[] Regex tells the unwary reader two thingsIt contains a regular expressionIt is the name of a classAnd both are incorrect.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9947
|
|
Campbell Ritchie wrote:But calling your String[] Regex tells the unwary reader two things It contains a regular expressionIt is the name of a classAnd both are incorrect.
To expand on Campbell's point...
Traditionally, in Java, class names start with a capital letter, and variable start with a lower-case letter. Many people seeing "Regex" (note the capital 'R') would think this was a class name.
Second, the word 'regex' is a commonly used term for a 'regular expression'. If i was reading your code and saw a variable named that, I would expect it to contain a regular expression. If I saw it was an array of strings, I'd expect each element to contain a regular expression.
Your array contains the tokens after splitting up a string based on a single character. You don't even use a regex anywhere that I see.
A better name for the variable would be something like "tokens" or "myTokens" or something along those lines. The variable should be named for what it CONTAINS, not how you got the data you stick in it.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32665
|
|
Maybe you were following the naming conventions in this article, which you should read when you have enough time
|
 |
 |
|
|
subject: Idea to sort a list and get a result
|
|
|