Solved

Java string 2D array

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

752 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