简体   繁体   English

JAVA:按字典顺序比较两个字符串

[英]JAVA: Lexicographically comparing two Strings

I am attending an IT school and we just started studying JAVA and we had to do a project that sort of simulated simple "messages" being passed around a network of Nodes that had IP Addresses. 我正在一所IT学校上学,我们才刚刚开始学习JAVA,我们必须做一个项目,在具有IP地址的节点网络中传递一种模拟的简单“消息”。

The whole project does not matter for purposes of this question. 就此问题而言,整个项目并不重要。

We have an IPAddress class, and one of its methods is to compare three IP address objects and decide whether the first one is "in the range" of the other two. 我们有一个IPAddress类,其方法之一是比较三个IP地址对象,并确定第一个是否在其他两个的“范围内”。

For example, let's have a Node in the system, call it N1 (there is a whole node class but that's unimportant atm) and have a package(also its own class) with the message "Hello" in it. 例如,让我们在系统中有一个Node,将其命名为N1(有一个完整的节点类,但是那不是很重要的atm),并有一个带有消息“ Hello”的包(也是它自己的类)。

The package has the IP Address of 127.0.0.1 程序包的IP地址为127.0.0.1

The system wants to pass it onto N1 which has an IP address of something (irrelevant) and an IP address range (which shows which range of IPs does it handle) of 系统希望将其传递到N1,该N1具有某个IP地址(无关)和一个IP地址范围(显示其处理的IP范围)。

127.0.0.0 (lower limit) and 127.0.0.255(upper limit) 127.0.0.0(下限)和127.0.0.255(上限)

The IPAddress class InsideRange method receives two IPs as parameters to compare it with the third in the following way: IPAddress类InsideRange方法接收两个IP作为参数,通过以下方式将其与第三个IP进行比较:

public boolean insideRng(IPAddress lower, IPAddress upper){
    String this_str = this.toString();
    String lower_str = lower.toString();
    String upper_str = upper.toString();
    String[] addressses = {this_str, lower_str, upper_str}; 
    for (int i=0; i<addressses.length; i++){
        String[] tmp = addressses[i].split("\\."); 
        addressses[i] = String.format("%3s.%3s.%3s.%3s",tmp[0],tmp[1],tmp[2],tmp[3]); 
    }
    String address = addressses[0]; 
    Arrays.sort(addressses);
    return (addressses[1].equals(address));
} 

In this case 127.0.0.1 IS INSIDE 127.0.0.0 and 127.0.0.255 so it returns true. 在这种情况下,127.0.0.1位于127.0.0.0和127.0.0.255之内,因此它返回true。

I did not make the above code however, I asked a friend to help me. 我没有编写上面的代码,但是我请一个朋友来帮助我。 The reason is that my version did not properly work (it returns true for an IP that is outside of the lower or upper limit sometimes): 原因是我的版本无法正常工作(对于超出下限或上限的IP有时返回true):

public boolean insideRng(IPAddress lower, IPAddress upper){ //nem mukodott
    String lower_str = lower.toString();
    String upper_str = upper.toString();
    String this_str = this.toString();
    return (lower_str.compareTo(this_str) <= 0 & upper_str.compareTo(this_str) >= 0);
} //(This is my version. THIS DOES NOT WORK)

My question is, why doesn't mine work? 我的问题是,为什么我的工作不进行? And: 和:

WHAT does this part do and why is the for loop needed at all 这部分是做什么的,为什么根本需要for循环

for (int i=0; i<addressses.length; i++){
    String[] tmp = addressses[i].split("\\."); 
    addressses[i] = String.format("%3s.%3s.%3s.%3s",tmp[0],tmp[1],tmp[2],tmp[3]);
}

why doesn't it work if I just go: 如果我刚去,为什么它不起作用:

public boolean insideRng(IPAddress lower, IPAddress upper){
    String this_str = this.toString();
    String lower_str = lower.toString();
    String upper_str = upper.toString();
    String[] addressses = {this_str, lower_str, upper_str};
    String address = addressses[0]; //address = "127.0.0.1";
    Arrays.sort(addressses);
    return (addressses[1].equals(address));
} 

(the toString() method does not call the original java toString method used on strings but instead IPadddress has an overridden toString() method which I will paste here:) (toString()方法不会调用用于字符串的原始java toString方法,而是IPadddress具有重写的toString()方法,我将在此处粘贴它:)

@Override
public String toString(){
    StringBuilder a = new StringBuilder();
    for(int i = 0; i < this.address.length -1; ++i){ 
        a.append(this.address[i]).append(".");
    }
    a.append(address[address.length-1]);
    return a.toString(); 
}

If I understand correctly, you are asking why comparing IP Strings such as these doesn't work : 如果我理解正确,您在问为什么比较像这样的IP字符串不起作用:

"2.0.0.127" and "10.0.0.127" “ 2.0.0.127”和“ 10.0.0.127”

The answer is that any String that starts with 2 comes (lexicographically) after a String that starts with 1. 答案是,任何以2开头的字符串(按字典顺序)都在以1开头的字符串之后。

The for loop you mentioned takes care of the problem by padding the numbers that make the IP String with leading zeroes : 您提到的for循环通过将使IP字符串以前导零填充的数字来解决此问题:

So, when you are comparing these Strings: 因此,当您比较这些字符串时:

"002.000.000.127" and "010.000.000.127" “ 002.000.000.127”和“ 010.000.000.127”

You'll get the expected result (which is that 002 comes before 010). 您将获得预期的结果(即002在010之前)。

EDIT : 编辑:

As Dave correctly commented, the padding in the for loop is actually with SPACEs, not zeroes, so the compared Strings become : 正如Dave正确评论的那样,for循环中的填充实际上是使用空格而不是零,因此比较的字符串变为:

"--2.---.---.127" and "-10.---.---.127" “ --2 .---..--。127”和“ -10 .---.---。127”
Where '-' stands for SPACE. 其中“-”代表SPACE。

Fortunately, this gives the same comparison result as padding with zeros would. 幸运的是,这提供了与零填充相同的比较结果。

Eran answer is good, my opinion is that you are expecting to make your IPAdress class implementing Comparable Eran的回答很好,我的观点是,您希望使IPAdress类实现可比性。

And define your compareTo method to take care of the different adresses in your adresses array. 并定义您的compareTo方法以处理地址数组中的不同地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM