File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Idea to sort a list and get a result Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Idea to sort a list and get a result" Watch "Idea to sort a list and get a result" New topic
Author

Idea to sort a list and get a result

Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

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: 18985
    
    8

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: 93

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: 19059
    
  40

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: 93

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: 40027
    
  28
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: 93

Not able to do it, still trying
Aditya Sirohi
Ranch Hand

Joined: Jan 05, 2010
Posts: 93

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: 93

Idea struck and i got it
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40027
    
  28
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

    Joined: Oct 02, 2003
    Posts: 11494
        
      16

    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.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 40027
        
      28
    Maybe you were following the naming conventions in this article, which you should read when you have enough time
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Idea to sort a list and get a result