Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Creating an array with a list of possible answers

Posted on 2006-04-05
24
Medium Priority
?
534 Views
Last Modified: 2012-08-14
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.
0
Comment
Question by:DanBAtkinson
  • 12
  • 7
  • 5
24 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16386390
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
 

Author Comment

by:DanBAtkinson
ID: 16386432
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
 

Author Comment

by:DanBAtkinson
ID: 16386529
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 16386533
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
 
LVL 92

Expert Comment

by:objects
ID: 16386594
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 400 total points
ID: 16386602
Error there sorry, should be

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

Author Comment

by:DanBAtkinson
ID: 16386643
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16386669
Well i thought each array element was distinct - maybe you want to eliminate dups even *within* elements ..?
0
 

Author Comment

by:DanBAtkinson
ID: 16386685
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
 

Author Comment

by:DanBAtkinson
ID: 16386788
How would I remove the elements from the hashset if they are duplicate?
0
 
LVL 92

Expert Comment

by:objects
ID: 16386922
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
 

Author Comment

by:DanBAtkinson
ID: 16386937
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
 
LVL 92

Expert Comment

by:objects
ID: 16386949
you can create it where u define it:

Set stations = new HashSet();
0
 

Author Comment

by:DanBAtkinson
ID: 16387008
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
 
LVL 92

Accepted Solution

by:
objects earned 1600 total points
ID: 16387017
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
 

Author Comment

by:DanBAtkinson
ID: 16387091
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
 
LVL 92

Assisted Solution

by:objects
objects earned 1600 total points
ID: 16387119
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
 

Author Comment

by:DanBAtkinson
ID: 16387178
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
 
LVL 92

Expert Comment

by:objects
ID: 16387208
u could also use Math.max() and Math.min() instead of having if/then and  two loops
0
 

Author Comment

by:DanBAtkinson
ID: 16387345
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16387375
>>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
 

Author Comment

by:DanBAtkinson
ID: 16387381
Points split (5:1 - objects:CEHJ) with assists where appropriate.

Thanks to both CEHJ and objects for your help! It's much appreciated!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16387389
:-)
0
 

Author Comment

by:DanBAtkinson
ID: 16387420
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question