1

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 ?

4
  • 2
    The character is being cast to an int. Cast it to a Character manually. list.remove((Character) i.charAt(0)); Commented Jun 19, 2013 at 0:40
  • @Legend Please make that answer Commented Jun 19, 2013 at 0:42
  • 2
    Someone already got to it. It's not about the reputation, it's about sending a message. Commented Jun 19, 2013 at 0:45
  • @Legend I agree, but know you answer is actually the only one answering the question that's begin asked +1 to you Commented Jun 19, 2013 at 0:59

4 Answers 4

6

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.

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

5 Comments

If I cast to a char instead, shouldn't that work by java boxing/unboxing it ?
@Phoenix: Auto-boxing is exactly what (Character) i.charAt(0) is triggering. It's not exactly a cast because a cast from primitive to a reference type does not make sense. If I cast to a char instead... also doesn't make sense because i.charAt(0) is already a char.
@BheshGurung: There's a difference between autoboxing and casting. Boxing is taken care of for you between primitive types, so i.charAt(0) is a taken from char to Character when need be. Casting changes the type, so (Character) i.charAt(0) changes it to Character.
@Phoenix the problem with this case is that there are 2 ways the char can be casted. It can cast to an int or a Character, since both of those methods exist in the ArrayList. It then chooses to cast to an int which makes it go by index.
@Makoto: My understanding of casting is that it can happen between primitives (e.g. an int to double), which could be widening or narrowing, or between references (e.g. from SuperType to SubType or vice-versa), and of (auto-) boxing is to instantiate the corresponding reference type for a primitive type with the value in context.
2

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.

Comments

1

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;
}

4 Comments

Will set preserve the order in which they appear ? arraylist does hence i am using it
If you absolutely positively must preserve the order in which they come in, then you can use a LinkedHashSet in place of the normal HashSet.
It'd be O(n) function ? Do you know What would be the performance ?
Javadocs state that the performance for add is about constant time, so O(1). Iterating over a LinkedHashSet may be slightly slower than a HashSet, though.
0

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.

Comments

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.