提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
是另一个char数组的一个子集
/** 来 自 n o w j a v a . c o m**/ //package com.nowjava; import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] argv) throws Exception { char[] charArr1 = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c', 'o', 'm', 'a', '1', }; char[] charArr2 = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c', 'o', 'm', 'a', '1', }; System.out.println(isSubset(charArr1, charArr2));/*from 时代Java - nowjava.com*/ } public static boolean isSubset(char[] charArr1, char[] charArr2) { if (charArr1.length > charArr2.length) { return false; } List<Character> charList1 = toList(charArr1); List<Character> charList2 = toList(charArr2); // cannot do containsAll as there can be duplicate characters for (Character charValue : charList1) { if (charList2.contains(charValue)) { charList2.remove(charValue); } else { return false; } } return true; } private