Creating an array with a list of possible answers

Hi there.

It's a terribly cryptic title but I tried my best to do one.

I have an array of 16 stations and at each station, I have a list of lines (interchanges) which are available at each station.

Thus:

static String[] stationInterchange =
{
      "British Rail",
      "British Rail",
      "British Rail",
      "British Rail",
      "Piccadilly, British Rail",
      "British Rail",
      "Circle Metropolitan, Northern Piccadilly, British Rail",
      "Northern, British Rail",
      "Northern",
      "Bakerloo Central",
      "Jubilee Piccadilly",
      "Circle District, British Rail",
      "--",
      "British Rail",
      "Northern",
      "British Rail"
};

When a user says they want to go from station x to station y (this would be indicated by their index in the station array) then a list of possible interchanges along the way should output.

For example... If I am travelling from station[3] to station[5], I would like the output to be:
British Rail, Piccadilly

Instead, it is:
"British Rail",
"Piccadilly, British Rail",
"British Rail",

What can I do to make this output correctly?

Thanks in advance.
DanBAtkinsonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
do u mean u want the unique ones?

If you could use a set (eg. HashSet)
Loop thru each array member
   Use spit(",") to station names
   And add the station names to Set

Sets don't allow duopes so at the end your have a set of unique stations
0
DanBAtkinsonAuthor Commented:
objects: I don't want duplicate values. For example, the first four stations all have the same interchange.

When I go from station[0] to station[3], I don't want to see a list of the same station... Just one.

A hashset?! I'll have a look at that now and get back to you.

Thanks.
0
DanBAtkinsonAuthor Commented:
objects:

I remember why I had this trouble with Hashtables. My problem was that I was creating the arrays and hashtables in the class as statics to be referenced later.

I've not used hashsets before (only hastables). Can you please explain what I'll need to do to create so that I can reference them please?

Something along the lines of:
      static HashSet interchanges = new HashSet();
            interchanges.add(0,"British Rail");
            interchanges.add(1,"British Rail");
            interchanges.add(2,"British Rail");
            interchanges.add(3,"British Rail");
            interchanges.add(4,"Piccadilly");
            interchanges.add(4,"British Rail");

?
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

CEHJCommented:
You don't need to use split, but try

String fromTo(int fromIX, int toIx) {
    return new HashSet(new ArrayList(Arrays.asList(stationInterchange)).subList(fromIx, toIx)).toString.replaceAll("[\\[\\]]", "");
}
0
objectsCommented:
thats about right, you'd put your call to add() (it doesn't need an index) in a loop for the necessary range.
and call spit for multiples. So for each entry you would do:

interchanges.add(stationInterchange[i].spit(","));

0
CEHJCommented:
Error there sorry, should be

return new LinkedHashSet(new ArrayList(Arrays.asList(stationInterchange)).subList(ixFrom, ixTo + 1)).toString().replaceAll("[\\[\\]]", "");
0
DanBAtkinsonAuthor Commented:
CEHJ:

I get duplicate values. Is there something wrong?

Also, I had to edit the code to take into account journeys going up the track (or the toIx being higher than fromIx):

      static String fromTo(int fromIx, int toIx)
      {
      if (fromIx <toIx)
            return new LinkedHashSet(new ArrayList(Arrays.asList(stationInterchange)).subList(fromIx, toIx + 1)).toString().replaceAll("[\\[\\]]", "");
      else
            return new LinkedHashSet(new ArrayList(Arrays.asList(stationInterchange)).subList(toIx, fromIx + 1)).toString().replaceAll("[\\[\\]]", "");
      }


A query from station[0] to station[15] results in:
British Rail, Piccadilly, British Rail, Circle Metropolitan, Northern Piccadilly, British Rail, Northern, British Rail, Northern, Bakerloo Central, Jubilee Piccadilly, Circle District, British Rail,
0
CEHJCommented:
Well i thought each array element was distinct - maybe you want to eliminate dups even *within* elements ..?
0
DanBAtkinsonAuthor Commented:
CEHJ:
In the station array, every element was unique.

In the interchange array, there are several elements that are the same. An example being stationInterchange[0]-stationInterchange[3].
0
DanBAtkinsonAuthor Commented:
How would I remove the elements from the hashset if they are duplicate?
0
objectsCommented:
You don't need to, HashSet does not allow dupes. So if you try and add something that is already there it won't get added.  Thats why I suggested using a Set.
0
DanBAtkinsonAuthor Commented:
objects:

How do I create the set outside a function (ie, just after the class declaration)?

If I start adding to it after I declare it, I get identifier errors.
0
objectsCommented:
you can create it where u define it:

Set stations = new HashSet();
0
DanBAtkinsonAuthor Commented:
objects: How would I write it then?

I can't use a for loop outside a method:


      Set interchanges = new HashSet();
      for (int i=0;i<stationInterchange.length;i++)
      {
            interchanges.add(stationInterchange[i].split(","));
      }
0
objectsCommented:
you would add that to the method where you want to find the stations.
or add a new method if necessary

Set getStations()
{
     Set interchanges = new HashSet();
     for (int i=0;i<stationInterchange.length;i++)
     {
          interchanges.add(stationInterchange[i].split(","));
     }
     return interchanges;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DanBAtkinsonAuthor Commented:
Thanks.

I think that's close to the mark. Except I'm getting some strings which I've not seen the like of in Java when I run a query on it:

[[Ljava.lang.String;@1eed786, [Ljava.lang.String;@1cd2e5f, [Ljava.lang.String;@19f953d, [Ljava.lang.String;@cf2c80, [Ljava.lang.String;@6eb38a, [Ljava.lang.String;@1172e08, [Ljava.lang.String;@1729854, [Ljava.lang.String;@1fee6fc]
0
objectsCommented:
sorry, my problem. spit() returns an array (thats how java displays arrays by default)
you need to convert it to a List

  interchanges.addAll(Arrays.asList(stationInterchange[i].split(",")));
0
DanBAtkinsonAuthor Commented:
Thanks!

The code I've used is:

      static Set interchangers(int dept,int dest)
      {
            Set interchanges = new HashSet();
            if (dept<dest)
            {
                  for (int i=dept;i<dest;i++)
                        interchanges.addAll(Arrays.asList(stationInterchange[i].split(",")));
                  return interchanges;
            }
            else
            {
                  for (int i=dest;i<dept;i++)
                        interchanges.addAll(Arrays.asList(stationInterchange[i].split(",")));
                  return interchanges;
            }
      }

It makes a set of all the stations in the route. The if/else takes into account people travelling up the track and down it.

Thanks again!
0
objectsCommented:
u could also use Math.max() and Math.min() instead of having if/then and  two loops
0
DanBAtkinsonAuthor Commented:
That's right!

That's twice tonight I've felt quite dumb!

Not really for points but how do I remove the square brackets from the result of the interchanges?

Example:
[Jubilee, British Rail, Piccadilly, District, Bakerloo Central, Northern, Circle, , Metropolitan]

0
CEHJCommented:
>>do I remove the square brackets from the result of the interchanges?

My code part does that. You also need to order the Set as, if you print it., you'll find it out of order. Use LinkedHashSet
0
DanBAtkinsonAuthor Commented:
Points split (5:1 - objects:CEHJ) with assists where appropriate.

Thanks to both CEHJ and objects for your help! It's much appreciated!
0
CEHJCommented:
:-)
0
DanBAtkinsonAuthor Commented:
Assuming you mean: ".replaceAll("[\\[\\]]", "");", how do I implement it into this code?:
      static Set interchangers(int dept,int dest)
      {
            Set interchanges = new LinkedHashSet();
                  for (int i=Math.min(dept,dest);i<Math.max(dept,dest);i++)
                  {
                        interchanges.addAll(Arrays.asList(stationInterchange[i].split(",")));
                  }
                  return interchanges;
      }
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.