Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java string 2D array

Posted on 2009-05-20
1
Medium Priority
?
406 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 500 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

688 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