Solved

Java string 2D array

Posted on 2009-05-20
1
399 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

911 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

25 Experts available now in Live!

Get 1:1 Help Now