问题描述
我试图了解人与人之间的关系。但是,当我运行单元测试时,测试会永远运行,它不会得到结果,而且我的CPU使用率很高。
有人可以看到我的代码有什么问题吗?
I try to get the relationship between people. However, when I run unit test, the test runs forever, it doesn't get the result and my CPU usage was high.Could someone see what's wrong with my code?
字符串关系是字符串的多行输入,格式为A ,BC,D
其中 A
是 B
和<$ c $的父母c> C 是 D
的父级。
The string relations are multiple line inputs of string with in the format of "A , B C , D"
where A
is the parent of B
and C
is the parent of D
.
这是默认的构造函数代码和字符串格式的输入。我们不需要检查格式是否正确:
This is the default constructor for the code and the input in string format. We don't need to check if the format is correct:
public SeeRelations(String relations){
this.relations = relations;
}
这是帮助函数从格式化输入中获取字符串的每一行:
This the helper function to get each line of the string from the formatted input:
//helper function to get each line of the string
private ArrayList<String> lineRelations(){
int i;
ArrayList<String> lineRelations = new ArrayList<String>();
String[] lines = relations.split("\n");
for(i = 0; i < lines.length; i++){
lineRelations.add(lines[i]);
}
return lineRelations;
}
这是将输入格式化字符串中的所有关系放到arraylists的函数:
This is the function to put all the relations from the input formatted string to arraylists:
//helper function to put each of the relationship in arraylists
private ArrayList<ArrayList<String>> allRelations(){
int i;
ArrayList<ArrayList<String>> allRelations = new ArrayList<ArrayList<String>>();
ArrayList<String> lineRelations = lineRelations();
for(i = 0; i < lineRelations.size(); i++){
ArrayList<String> eachLine = new ArrayList<String>(Arrays.asList(lineRelations.get(i).split("\\s*,\\s*")));
allRelations.add(eachLine);
}
return allRelations;
}
这是检查输入名称是否存在的方法:
This is the method to check if the input name exists:
//helper function to see if the name exist for seeRelations()
private boolean hasThisName(String name){
ArrayList<ArrayList<String>> allRelations = allRelations();
int i;
int j;
for(i = 0; i < allRelations.size(); i++){
for(j = 0; j < allRelations.get(i).size(); j++){
if(name.equals(allRelations.get(i).get(j))){
return true;
}
}
}
return false;
}
这是获取两个人之间世代号的函数:
This is the function to get the generation number between two people:
//helper function to get Generation number of seeRelations()
private int getGenerationNum(String person, String ancestor){
ArrayList<ArrayList<String>> allRelations = allRelations();
String name;
int i;
int j;
int generationNum = 0;
for(i = 0, j = 0, name = ancestor; i < allRelations.size(); i++){
if(name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1))){
generationNum++;
ancestor = allRelations.get(i).get(1);
i = 0;
j = 1;
}
else if(ancestor.equals(allRelations.get(i).get(0)) && person.equals(allRelations.get(i).get(1))){
generationNum++;
j = 1;
break;
}
}
if(j == 0){
return 0;
}
else{
return generationNum;
}
}
这是获取<$ c的倍数的方法$ c>great用于最终输出:
This is the method to get multiple of "great"
for the final output:
private String great(int num){
int i;
String great = "";
for(i = 0; i < num; i++){
great += "great";
}
return great;
}
这是我检查两个人之间关系的最终方法:
This is my final method to check the relationship between two people:
public String SeeRelations(String person, String ancestor){
int generationNum = getGenerationNum(person, ancestor);
String great = great(generationNum - 2);
if(!(hasThisName(person) && hasThisName(ancestor))){
return null;
}
else{
if(generationNum == 0){
return null;
}
else if(generationNum == 1){
return ancestor + " is the parent of " + person;
}
else if(generationNum == 2){
return ancestor + " is the grandparent of " + person;
}
else{
return ancestor + " is the" + " " + great +"grandparent of " + person;
}
}
}
这是我的测试用例,并且它永远运行并且无法获得结果
This is my test cases, And it runs forever and couldn't get a result
public class FamilyTreeTest {
@Test
public void testSeeRelations() {
FamilyTree relation2 = new FamilyTree("John Doe , Mary Smith" + "\n" + "Martin Weasel , John Doe");
assertEquals("Martin Weasel is the grandparent of Mary Smith", familyTree2.SeeRelations("Mary Smith", "Martin Weasel"));
}
推荐答案
for(i = 0, j = 0, name = ancestor; i < allRelations.size(); i++){
if(name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1))){
generationNum++;
ancestor = allRelations.get(i).get(1);
i = 0;
j = 1;
}
else if(ancestor.equals(allRelations.get(i).get(0)) && person.equals(allRelations.get(i).get(1))){
generationNum++;
j = 1;
break;
}
}
这里有错误的行
in你的情况你的祖先/名字是Martin Weasel,因为马丁的关系是John Doe,但你正在寻找玛丽史密斯所以 name.equals(allRelations.get(i).get(0) )&& !person.equals(allRelations.get(i).get(1)))
这是 true
并且这个 i = 0;
让你的循环从头开始
here you have your faulty linesin your case your ancestor/name is "Martin Weasel" given relation for martin is "John Doe", but you are looking for mary smith so name.equals(allRelations.get(i).get(0)) && !person.equals(allRelations.get(i).get(1)))
this is true
and this i = 0;
makes your loop starts from beginning
你可以做什么,尝试创建对象人
即
class Person {
String name;
列出儿童;
列出父母;
...
}
然后只做简单的树步行者
what you could do, try to create object personieclass Person{String name;List childrens;List parents;...}then just do simple tree walker
int SearchDown(Person person, String searchedRelation,int generation)
{
if person.getName().equals(searchedRelation())
return generation;
for (Person child: person.getChildren())
{
int generation = SearchDown(child, searchedRelation, generation+1);
if (generation!=-1) return generation;
}
return -1;
}
等......
我真的觉得这种方式更容易处理所有类型的树
i'm really finding this way much easier to deal with all types of trees
这篇关于带有测试的Java代码 - 无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!