Java 如何在新数组中存储匹配字符
我正在尝试将一个元素存储在一个新数组中。下面是我的代码Java 如何在新数组中存储匹配字符,java,arrays,Java,Arrays,我正在尝试将一个元素存储在一个新数组中。下面是我的代码 for (int i = 0; i < namea1.length; i++) { for (int j = 0; j < namea2.length; j++) { if (namea1[i] == namea2[j]) { // ans=new char[shojib[i]]; count++; } } } 我想将所有匹配字符存
for (int i = 0; i < namea1.length; i++) {
for (int j = 0; j < namea2.length; j++) {
if (namea1[i] == namea2[j]) {
// ans=new char[shojib[i]];
count++;
}
}
}
我想将所有匹配字符存储到新数组。尝试使用
ArrayList
public static void main(String[] args) {
String name1 = "sfhojib";
String name2 = "farhana";
int count = 0;
char[] namea1 = name1.toCharArray();
char[] namea2 = name2.toCharArray();
List list = new ArrayList();
for (int i = 0 ; i < namea1.length ; i++) {
for (int j = 0 ; j < namea2.length ; j++) {
if (namea1[i] == namea2[j]) {
list.add(namea1[i]);
}
}
}
for (int i = 0 ; i < list.size() ; i++) {
System.out.println(list.get(i));
}
}
尝试使用ArrayList。试试下面
List<Character> match = new ArrayList<Character>();
//...
// If chars are matching
match.add(namea1[i]);
List match=new ArrayList();
//...
//如果字符匹配
匹配。添加(名称1[i]);
如果只需要唯一的匹配字符,那么使用Set
如何:
示例代码:
import java.util.HashSet;
import java.util.Set;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("temp", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
Set<Character> namea1Chars = new HashSet<Character>();
Set<Character> namea2Chars = new HashSet<Character>();
for (char c : a.toCharArray()) {
namea1Chars.add(c);
}
for (char c : b.toCharArray()) {
namea2Chars.add(c);
}
namea1Chars.retainAll(namea2Chars);
System.out.println("Common Chars in " + a + " & " + b + " are => " + namea1Chars);
}
}
如果要捕获所有事件,请使用列表
(尽管会影响性能:
示例代码:
import java.util.ArrayList;
import java.util.List;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("tempt", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
List<Character> namea1Chars = new ArrayList<Character>();
List<Character> namea2Chars = new ArrayList<Character>();
for (char c : a.toCharArray()) {
namea1Chars.add(c);
}
for (char c : b.toCharArray()) {
namea2Chars.add(c);
}
namea1Chars.retainAll(namea2Chars);
System.out.println("Common Chars in " + a + " & " + b + " are => " + namea1Chars);
}
}
import java.util.HashMap;
import java.util.Map;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("tempt", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
System.out.print("Common Chars in " + a + " & " + b + " are => [");
Map<Character, Integer> aCharCount = new HashMap<Character, Integer>();
for (char c : a.toCharArray()) {
if(aCharCount.containsKey(c)) {
aCharCount.put(c, aCharCount.get(c) + 1);
} else {
aCharCount.put(c, 1);
}
}
for (char c : b.toCharArray()) {
if(aCharCount.containsKey(c)) {
System.out.print(c + ", ");
if (aCharCount.get(c) == 1) {
aCharCount.remove(c);
} else {
aCharCount.put(c, aCharCount.get(c) - 1);
}
}
}
System.out.println("]");
}
}
您还可以使用Map
以获得更好的性能
示例代码:
import java.util.ArrayList;
import java.util.List;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("tempt", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
List<Character> namea1Chars = new ArrayList<Character>();
List<Character> namea2Chars = new ArrayList<Character>();
for (char c : a.toCharArray()) {
namea1Chars.add(c);
}
for (char c : b.toCharArray()) {
namea2Chars.add(c);
}
namea1Chars.retainAll(namea2Chars);
System.out.println("Common Chars in " + a + " & " + b + " are => " + namea1Chars);
}
}
import java.util.HashMap;
import java.util.Map;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("tempt", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
System.out.print("Common Chars in " + a + " & " + b + " are => [");
Map<Character, Integer> aCharCount = new HashMap<Character, Integer>();
for (char c : a.toCharArray()) {
if(aCharCount.containsKey(c)) {
aCharCount.put(c, aCharCount.get(c) + 1);
} else {
aCharCount.put(c, 1);
}
}
for (char c : b.toCharArray()) {
if(aCharCount.containsKey(c)) {
System.out.print(c + ", ");
if (aCharCount.get(c) == 1) {
aCharCount.remove(c);
} else {
aCharCount.put(c, aCharCount.get(c) - 1);
}
}
}
System.out.println("]");
}
}
在循环外部创建数组,或者每次获得匹配字符时都创建一个新数组 您可以使用
ArrayList
,因为您不确定数组的大小(然后使用list.toArray(…)
),但您的基本字符将在每次添加/从列表中获取时自动装箱和取消装箱。如果您不想这样做,最好的选择是使用char[]最长名称的长度
包含匹配项,然后手动修剪
至于匹配,您可以将字母添加到列表中,然后将其从每个单词中删除,尽管您需要某种类型的“空”字符来替换字母,您知道这些字母不会出现在任何名称中
不使用ArrayList
char[] name1 = "shojaaaaib".toCharArray();
char[] name2 = "farhana".toCharArray();
char[] matches = new char[name2.length];
final char placeHolder = '~';
firstName:
for(int i = 0; i < name1.length; i++)
for(int j = 0; j < name2.length; j++)
if(name1[i] == name2[j])
for(int k = 0; k < matches.length; k++) //add to matches index if possible
if(matches[k] == 0) { //if not set yet
matches[k] = name1[i];
name1[i] = name2[j] = placeHolder;
continue firstName;
}
System.out.println(matches);
由于有1个匹配的
h
和2个匹配的a@JunedAhsan,h
是常见的。不确定这是否是最好的解决方案,因为它只会告诉哪些字母匹配,而不会告诉发生了多少匹配。您是否检查HashMap
解决方案上的性能差异?我不确定我的结果是否因为使用移动IDE,但是可以看到您是如何自动装箱输入到地图中的每个键和值的。您应该检查并报告时间上的差异
import java.util.HashMap;
import java.util.Map;
public class IntersectionChar {
public static void main(String[] args) {
findMatchingChars("shojib", "farhana");
findMatchingChars("one", "onetwothere");
findMatchingChars("tempt", "test");
findMatchingChars("dis", "connected");
findMatchingChars("null", "empty");
}
private static void findMatchingChars(String a, String b) {
System.out.print("Common Chars in " + a + " & " + b + " are => [");
Map<Character, Integer> aCharCount = new HashMap<Character, Integer>();
for (char c : a.toCharArray()) {
if(aCharCount.containsKey(c)) {
aCharCount.put(c, aCharCount.get(c) + 1);
} else {
aCharCount.put(c, 1);
}
}
for (char c : b.toCharArray()) {
if(aCharCount.containsKey(c)) {
System.out.print(c + ", ");
if (aCharCount.get(c) == 1) {
aCharCount.remove(c);
} else {
aCharCount.put(c, aCharCount.get(c) - 1);
}
}
}
System.out.println("]");
}
}
Common Chars in shojib & farhana are => [h, ]
Common Chars in one & onetwothere are => [o, n, e, ]
Common Chars in tempt & test are => [t, e, t, ]
Common Chars in dis & connected are => [d, ]
Common Chars in null & empty are => []
char[] name1 = "shojaaaaib".toCharArray();
char[] name2 = "farhana".toCharArray();
char[] matches = new char[name2.length];
final char placeHolder = '~';
firstName:
for(int i = 0; i < name1.length; i++)
for(int j = 0; j < name2.length; j++)
if(name1[i] == name2[j])
for(int k = 0; k < matches.length; k++) //add to matches index if possible
if(matches[k] == 0) { //if not set yet
matches[k] = name1[i];
name1[i] = name2[j] = placeHolder;
continue firstName;
}
System.out.println(matches);
haaa