Solved

Java string 2D array

Posted on 2009-05-20
1
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

617 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