Comparing two strings

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
saregg
Posts: 20
Joined: Fri May 01, 2020 11:27 am
Contact:

Comparing two strings

Post by saregg »

I wanted to compare two strings and return true if one string contains all the char of the other

Code: Select all

public boolean canConstruct(String ransomNote, String magazine) {
        char[] ca = magazine.toCharArray();
        List<char[]> ch = new ArrayList<>(Arrays.asList(ca));
        char[] cb = ransomNote.toCharArray();
        List<char[]> de = new ArrayList<>(Arrays.asList(cb));
        return de.containsAll(ch)? true:false;
    	} 
But this always return false, why ?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Comparing two strings

Post by admin »

Check out how containsAll of List works. what does it compare.

de.containsAll simply checks if all the elements of ch are there in de. de doesn't contain chars. It contains an array.

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests