Solved

Java string 2D array

Posted on 2009-05-20
1
398 Views
Last Modified: 2013-11-23
I have a string with this as the input: abcd efgh ijkl abld abem
now this can be random
i want to  split this using string.split[] and i want to create a 2 dim array to input this in a way that all the elements starting with ab is in the 1st array of 2 dim
like if it is in String[2][2] so to search for this one will first look at string[1][0] so in string[1]....all the elements with ab would be stored.

I hope i tried to make things clear
0
Comment
Question by:SunScreenCert
1 Comment
 
LVL 3

Accepted Solution

by:
hazgoduk earned 125 total points
ID: 24434358
I'd use a HashMap<String, Vector> or something.

Do the split so you have ab and cd as 2 seperate strings, then you can store them as follows.

To get them back again you can just say Vector values = map.get(ab); to give you  Vector with all the elements that started with ab.

Change to HashMap if you want the elements to be unique and LinkedHashMap if you want them to remain in order
String part1 = "ab";

String part2 = "cd";
 

public void store(String part1, String part2)

{

  Vector values = null;

  if(map.contains(part1))

  {

    values = map.get(part1);

  }

  else

  {

    values = new Vector();

    map.put(part1, values);

  }
 

  values.add(part2);

}

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now