1

I have an ArrayList where I add Strings like [string1 string2 string3 code]. This code is just a number I give to each string. But this code is what I have to use to delete or use a string.

So lets suppose I have a arrayList with the next strings:

String_1 = string1 string2 string3 1
String_2 = string4 string5 string6 2
String_3 = string7 string8 string9 3

Now, if I want to delete String_2 I have to do: delete string "code" in this case, delete string 2. this would delete the String_2 from the arrayList. Also, if I want to extract String_1 from the arraylist and set it in a String, I have to do it using the code.

So, what will be the way to do this in java? Maybe is something easy for many of you, but I'm new with this and arrayList is still something a bit dificult for me.

3 Answers 3

3

First of all, this is more a use case for a map, where your code is the key and the string is the value. See http://docs.oracle.com/javase/7/docs/api/java/util/Map.html.

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(1, "string1");
map.put(2, "string2");
map.get(1)
...

Second approach is to create a new object holding the code as an Integer and the String, like

public class StringHolder {
int code;
String string;

// constructor
// setter and getter
}

then use a for-each loop to iterate trough your array list and compare the code of the object with the given one.

Or, the rude, not recommended way: do some crazy string operations to figure out where your code is in the string. Acutally, do not do this.

Sign up to request clarification or add additional context in comments.

5 Comments

I can't use this map method, I have to create an ArrayList<OwnClass> because I have to reference to other class constructor. So, another way to do it without map?
Also i have to be able to list all the strings from the arraylist showing on the console each string's code after the string
You can reference your own class in a map too: Example: Map<Integer, OwnClass> myMap = new HashMap<Integer, OwnClass>()
Show us OwnClass!Or change it like my class StringHolder
I may be wrong but would an ArrayList<String> be more suitable for the map value, than just a string. Thats what the example looks like
2

If you must do it with an ArrayList I would suggest something like this:

public static void main(String[] args) {
    List<String> yourData = new ArrayList<String>();
    yourData.add("string1 string2 string3 1");
    yourData.add("string4 string5 string6 2");
    yourData.add("string7 string8 string9 3");

    Iterator<String> dataIterator = yourData.iterator();
    while(dataIterator.hasNext()) {
        String d = dataIterator.next();
        if(d.endsWith("2")) {
            dataIterator.remove();
        }
    }
}

This assumes the code is a string, and is always the last word of your data.

Comments

1

Try this:

List<String> stringsArray = new ArrayList<String>();
stringsArray.add("string1 string2 string3 1");
stringsArray.add("string4 string5 string6 2");
stringsArray.add("string7 string8 string9 3");
Iterator<String> iter = stringsArray.iterator();

while( iter.hasNext() ) {
String s = iter.next();
if( s.endsWith("1") ) {
   iter.remove();
  }
}

1 Comment

This will cause a concurrent modification exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.