【问题标题】:Swapping names using indexOf and substring and compareTo使用 indexOf 和 substring 和 compareTo 交换名称
【发布时间】:2010-04-29 19:17:18
【问题描述】:

我无法理解如何使用 indexOf()substring()compareTo() 方法在数组中将人们的名字与他们的姓氏翻转。

【问题讨论】:

  • 您能否发布您要解决的问题的完整描述? indexOf、substring 和 compareTo 并没有真正用于更改数组中的内容,听起来您正在尝试解决一些更大的问题。
  • 您能详细说明一下这个问题吗?这个数组中有哪些值?
  • 嗯,这是一个我遇到问题的课程的程序。我有一个名字数组,里面有名字和姓氏,我们的教授试图向我们解释如何使用 indexof 来查找名字和子字符串之间的空间,而 compaeto 也不确定它是如何组合在一起的
  • 例如 String[] names={"Mary Smith","John Cummings","Herald Williams","Debbie Boile","Ralph Chester"};我需要用那里的名字切换姓氏,以便稍后按姓氏排序
  • 如果你认真学习,试试codingbat.com/java;它有数百个适合初学者的练习,包括基本的String 操作部分

标签: java string swap


【解决方案1】:

假设您有以下内容:

String[] names = new String[]{"Joe Bloggs", "Sam Sunday"};

您可以使用以下代码交换姓氏和名字:

for (int i=0; i < names.length; i++)
{
    String someName = names[i];
    int spaceBetweenFirstAndLastName = someName.indexOf(" ");
    //These next two lines of code may be off by a character
    //Grab the characters starting at position 0 in the String up to but not including
    //   the index of the space between first and last name
    String firstName = someName.substring(0, spaceBetweenFirstAndLastName);
    //Grab all the characters, but start at the character after the space and go 
    //   until the end of the string
    String lastName = someName.substring(spaceBetweenFirstAndLastName+1);
    //Now, swap the first and last name and put it back into the array
    names[i] = lastName + ", " + firstName;
}

字符串compareTo 方法现在可用于通过比较一个名称与另一个名称来对名称进行排序,现在姓氏是字符串的开头。看看 api here 看看能不能搞定。

【讨论】:

    【解决方案2】:

    你可以使用 split 方法将名称分隔成一个字符串数组,考虑到它们是用空格分隔的。

    例如,让我们考虑一个名字:

    String name = "Mario Freitas";
    
    String[] array = name.split(" "); // the parameter is the string separator. In this case, is the space
    
    for(String s : array){
        System.out.println(s);
    }
    

    此代码将在不同的行中打印每个名称(因为字符串是分开的)

    然后你可以使用equals方法比较名字和姓氏。

    假设您有 2 个字符串数组,通过 split 方法获得,每个字符串都有一个不同的人名。

    public void compare names(String name1, String name2){
        String array1[] = name1.split(" ");
        String array2[] = name2.split(" ");
    
        if(array1[0].equals(array2[0])){
            System.out.println("First names are equal");
        }
    
        if(array1[1].equals(array2[1])){
            System.out.println("Second names are equal");
        }
    }
    

    【讨论】:

    • 所有的名字都已经存储在一个字符串数组中我需要翻转它们并将它们放回同一个数组中
    • 您可以使用拆分方法翻转名称。只需创建另一个字符串,用翻转数组的值填充它。然后你就有了翻转的名字。例如:字符串 s = array1[1] + " " + array1[0];
    【解决方案3】:

    您可以使用正则表达式,特别是 String.replaceAll(String regex, String replacement) 来重新排列名字和姓氏。

        String[] names = { "John A. Doe", "James Bond" };
        for (int i = 0; i < names.length; i++) {
            names[i] = names[i].replaceAll("(.*) (.*)", "$2, $1");
            System.out.println(names[i]);
        }
    

    打印出来:

    Doe, John A.
    Bond, James
    

    但是,如果您交换的唯一原因是因为您以后想对姓氏进行排序,那么您实际上根本不需要交换。只需将提取姓氏的代码封装到一个辅助方法中(因此它是可测试的、可重用的等),然后在您的自定义 java.util.Comparator for java.util.Arrays.sort 中使用它

    import java.util.*;
    
    //...
    
    static String lastName(String name) {
        return name.substring(name.lastIndexOf(' '));
    }
    
    //...
    
    String[] names = { "John A. Doe", "James Bond" };       
    Comparator<String> lastNameComparator = new Comparator<String>() {
        @Override public int compare(String name1, String name2) {
            return lastName(name1).compareTo(lastName(name2));
        }           
    };
    Arrays.sort(names, lastNameComparator);
    for (String name : names) {
        System.out.println(name);
    }
    

    打印出来:

    James Bond
    John A. Doe
    

    请注意,在这两个 sn-ps 中,它是空格字符的 last 索引,用于定义名字和姓氏之间的边界。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-09
      • 2015-03-03
      • 2011-08-18
      • 2019-10-04
      • 1970-01-01
      • 2023-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多