[英]Compare Java arrays
I'm trying to compare the names of the roomtypes in a simple Java program but I can't get it to work. 我正在尝试在一个简单的Java程序中比较房间类型的名称,但无法正常工作。
So far I have this: 到目前为止,我有这个:
public class Main {
public static void main(String[] args) {
RoomType[] ar = new RoomType[10];
String s = "";
String duplicate = "";
ar[0] = new RoomType("Standaard", 2, 60.0);
ar[1] = new RoomType("DeLuxe", 2, 85.0);
ar[2] = new RoomType("DeLuxe", 4, 125.0);
ar[3] = new RoomType("Hiker", 2, 35.0);
for (int i = 0; i < ar.length; i++) {
if (ar[i] != null) {
s += ar[i] + "\n";
}
for (int j = 0; j < ar.length -1; j++) {
if (ar[i] == ar[j] && ar[i] != null && ar[j] != null) {
duplicate = ar[i] + " has the same name as " + ar[j];
}
}
}
System.out.println("These are the roomtypes: \n" + s + "\n");
System.out.println(duplicate);
}
} }
I want to compare the names (the first elements in the arrays) of all ar[]
's and if there are doubles I need a sysout that gives the position ( ar[]
) of the doubles. 我想比较所有
ar[]
的名称(数组中的第一个元素),如果有双精度型,我需要一个sysout给出双精度型的位置( ar[]
)。 The method getTypeName() is in a different class RoomType. 方法getTypeName()在另一个类RoomType中。
"I want to compare the names (the first elements in the arrays) of all ar[]'s and if there are doubles I need a sysout that gives the position (ar[]) of the doubles"
“我想比较所有ar []的名称(数组中的第一个元素),如果有双精度型,我需要一个sysout给出双精度型的位置(ar [])”
It seems like your approach is wrong. 看来您的方法是错误的。 Your
RoomType
could implement equals
and hashcode
based on your requirements and then you can add them to a data structure that doesn't permit duplicates like java.util.Set
. 您的
RoomType
可以根据您的要求实现equals
和hashcode
,然后可以将其添加到不允许重复的数据结构中,例如java.util.Set
。 Trying to insert a duplicate element to a Set will return false
and this could help you. 尝试将重复元素插入到Set中将返回
false
,这可以为您提供帮助。 Unless you are practicing/playing around with arrays in Java, standard data structures will solve these kind of problems for you. 除非您正在使用Java练习/研究数组,否则标准数据结构将为您解决这类问题。
A couple of things to note: 需要注意的几件事:
StringBuilder
: http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html to "grow" Strings. StringBuilder
可以节省更多的空间/时间: http : //docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html “增长”字符串。 .equals()
instead. .equals()
代替。 HashMap<String, ArrayList<Integer>>
to efficiently store duplicate info. HashMap<String, ArrayList<Integer>>
有效地存储重复信息。 We map type names to positions in the array. public void printDuplicateInfo(RoomType[] roomList) {
HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>();
for(int i = 0; i < roomList.length; i++) {
ArrayList<Integer> pos;
String name = roomList[i].getTypeName()
if(!map.containsKey(name))
pos = new ArrayList<Integer>();
else
pos = map.get(name);
pos.add(i);
map.put(name, pos);
}
Set<String> keys = map.keySet();
StringBuilder strBuf = new StringBuilder();
strBuf.append("***Duplicate Info***\n");
for(String name : set) {
ArrayList<Integer> pos = map.get(name);
int size = pos.size();
if(size == 1)
continue;
strBuf.append(name).append(": present at indices ");
for(int i = 0; i < size; i++)
strBuf.append(pos.get(i)).append(" ");
strBuf.append("\n")
}
System.out.print(strBuf);
}
Here is what I interpreted as what you are asking for: 这就是我要解释的:
for(int i = 0; i < ar.length; i++)
{
if(ar[i] != null)
s += ar[i].getTypeName()+"\n";
for(int j = 1; j < ar.length; j++)
{
if(ar[i] != null && ar[j] != null && ar[i].getTypeName().equals(ar[j].getTypeName()))
{
duplicate += i+" has the same name as "+j+"\n";
}
}
}
System.out.println("These are the roomtypes: \n"+s+"\n");
System.out.println(duplicate);
Try using ArrayList, and .Contains to compare. 尝试使用ArrayList和.Contains进行比较。 It will make your life easier and get you introduced to more datastructures in Java.
这将使您的生活更轻松,并为您介绍Java中的更多数据结构。
All the best with the school. 祝学校一切顺利。 this is a great place to grow.
这是一个成长的好地方。 Be active :)
积极点 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.