This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Idea to sort a list and get a result

 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The input are in an array. So i wrote the code like this as below. Does not work!!

-Aditya

 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But don't call the String[] Regex. That will confuse anybody who reads your code quickly.
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not able to do it, still trying
 
Aditya Sirohi
Ranch Hand
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 93
Eclipse IDE Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Idea struck and i got it
 
Campbell Ritchie
Sheriff
Posts: 48910
58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But calling your String[] Regex tells the unwary reader two things
  • It contains a regular expression
  • It is the name of a class
  • And both are incorrect.
     
    fred rosenberger
    lowercase baba
    Bartender
    Posts: 12122
    30
    Chrome Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote:But calling your String[] Regex tells the unwary reader two things
  • It contains a regular expression
  • It is the name of a class
  • And 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.

     
    Campbell Ritchie
    Sheriff
    Posts: 48910
    58
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Maybe you were following the naming conventions in this article, which you should read when you have enough time
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic