Phoenix
Phoenix

Reputation: 8923

Java ArrayList not working on characters in my case

I have a big doubt. I want to find the first char of a string here which isn't repeated.For e.g. for the input below should return 'c'. So this is how I was planning on doing it. But I noticed the remove method is looking to remove at an index of 98 vs removing the object "a". How do I force it to remove the object "a" instead of removing from index ?

Why doesn't this work ?

And what can I do to change this ?

Is ArrayList always guaranteed to store things in order ?

public void findStartingLetter()
{
        String[] array={"a","b","c","d","b","a","d","d","d"};

        List<Character> list = new ArrayList<Character>();



        for(String i:array)
        {
            if(list.contains(i.charAt(0)))
                list.remove(i.charAt(0));
            else
                list.add(i.charAt(0));

        }

    }

EDIT:

Performance wise is this an O(n) function ?

Upvotes: 1

Views: 594

Answers (4)

Makoto
Makoto

Reputation: 106390

I'm not entirely sure why you want to use a List for this, but I would instead recommend a Set - it's guaranteed to not contain duplicates.

Here's the first approach, with a set:

public Set<Character> addToSet(String[] elements) {
    Set<Character> res = new HashSet<>();
    for(String c : elements) {
        res.add(c.charAt(0));
    }
    return res;
}

Now, if you really want to do this with a List, then it's similar code - you just need to check to see if the element exists before you add it in.

public List<Character> addUnique(String[] elements) {
    List<Character> res = new ArrayList<>();
    for(String item : elements) {
        Character c = item.charAt(0);
        if(!res.contains(c)) {
            res.add(c);
        }
    }
    return res;
}

Upvotes: 1

user1181445
user1181445

Reputation:

You have to cast manually to a Character since the char gets casted to an int, which in turn goes by index and not value.

list.remove((Character) i.charAt(0));

Will ensure that it is done properly.

Upvotes: 6

Aurand
Aurand

Reputation: 5537

Your approach to this problem is quite confusing and you ask many questions which do not seem to relate to your problem.

Why not just use:

String testString = "abcdbaddd";
Character retVal = null;
for (int i = 0; i < testString.length() -1; i++) {
    if (testString.charAt(i) == testString.charAt(i + 1)) {
        retVal = testString.charAt(i);
        break;
    }
}
return retVal;

That gets you the first non-repeated character (I'm assuming that by repeated you mean repeated and adjacent) or null if no such character exists.

Upvotes: 0

Java Devil
Java Devil

Reputation: 10959

Is ArrayList always guaranteed to store things in order ?

Depends on your definition of order: If you mean the order you add them, Yes. If you mean numerical/alphabetical order, then No, but you can sort it by using

Collections.sort(list)

This will sort by the natural ascending order of the objects in the list.

Upvotes: 2

Related Questions