在java中查找数组的最高索引

时间:2013-12-26 20:09:08

标签: java arrays search

所以我对java比较陌生,而且我做了一个小应用程序,它接收来自用户的输入(电话号码和姓名)。 这些值保存在一个对象数组下(在请求输入后...):

for(int i=0; i<users.length;i++){
users[i]= new id(name, phone);
}

我遇到的问题是搜索功能,这就是我所拥有的:

System.out.println("Enter users name: ");
nameIn =scan.next();
int [] simIndex = new int [100];

for(int z = 0; z<counter; z++){ //goes through all names
                //z tells us the name we are on
for(int o =0; o<nameIn.length() && o<users[z].name.length();o++){ //goes length of name
                    //o tells us the char we are at
char inputName = nameIn.charAt(o); //takes charAt increments
char indexName = users[z].name.charAt(o);
if(inputName == indexName){ //if inputed char and index char are the same
numSim++;
simIndex[z] =numSim; 

到目前为止,一切都有效。 我想要做的是打印出SimIndex []的最高值(那些将与输入最相似) 那么如何找到数组的最高值并得到它们的索引呢?

3 个答案:

答案 0 :(得分:1)

如果将值放入ArrayList,则可以使用Collections.sort

Java Collections Trail值得一读。了解基本集合类型,何时使用它们以及集合的方便内置功能将为您节省大量的编码工作和心痛。

答案 1 :(得分:1)

使用guava library,您可以使用Ints#max。它需要一个int []作为参数。

对于这种应用程序,最好使用TreeMap(而不是数组)。

答案 2 :(得分:0)

正如一些人已经说过,你可以根据输入的名字与你的名字相似来对你的名字进行排序。为此,您可以像现在一样将名称保存在数组中,但必须使用Comarator。这个比较器将采用两个名称并决定它们的相似程度。它将用于对您的名称数组进行排序。

更棘手的部分是确定两个字符串之间的相似性。计算这种相似性的一种方法是使用Levenshtein distance。我从wiki文章中复制了这个算法的“Iterative with two matrix rows”实现并将其翻译成Java(参见答案的底部)并在我的比较器中使用它:

// make sure that this is final so we can use it inside of the comparator
final String nameIn = scan.next();
Arrays.sort(names, 0, names.length, new Comparator<id>() {

  @Override
  public int compare(id o1, id o2) {
    String name1 = o1.name;
    String name2 = o2.name;

    // how different is name1 from nameIn?
    int name1Distance = levenshteinDistance(name1, nameIn);

    // how different is name2 from nameIn?
    int name2Distance = levenshteinDistance(name2, nameIn);

    // difference of the differences
    // if name1 and name2 are equally different from nameIn then this is 0
    // meaning that name1 and name2 are "equal"
    // if name1 is less different than name2 then this will be < 0
    // meaning that name1 is "less than" name2
    // if name1 is more different that name2 then this will be > 0
    // meaning that name1 is "more than" name2
    return name1Distance - name2Distance;
  }
});
// the names array is now sorted
// the first element in the array is least different from nameIn
// the last element in the array is most different from nameIn

如果你想得到一个排序索引数组而不是排序names数组,你可以用非常类似的方式来做:

// make sure that this is final so we can use it inside of the comparator
final String nameIn = scan.next();
// make a final reference to the names array so that it can be accessed in the comparator
final id[] namesFinal = names;
// this will be the array we will sort, it will hold the sorted indices of
// elements in the names array
final Integer[] sortedIndices = new Integer[names.length];

// initialize the sortedIndices array (index 0 is 0, index 1 is 1, etc)
for (int i = 0; i < sortedIndices.length; i++) {
    sortedIndices[i] = i;
}


Arrays.sort(sortedIndices, 0, names.length, new Comparator<Integer>() {

  @Override
  public int compare(Integer o1, Integer o2) {
    // get the names from the names array using o1 and o2 as indices
    String name1 = namesFinal[o1].name;
    String name2 = namesFinal[o2].name;

    // how different is name1 from nameIn?
    int name1Distance = levenshteinDistance(name1, nameIn);

    // how different is name2 from nameIn?
    int name2Distance = levenshteinDistance(name2, nameIn);

    // difference of the differences
    // if name1 and name2 are equally different from nameIn then this is 0
    // meaning that name1 and name2 are "equal"
    // if name1 is less different than name2 then this will be < 0
    // meaning that name1 is "less than" name2
    // if name1 is more different that name2 then this will be > 0
    // meaning that name1 is "more than" name2
    return name1Distance - name2Distance;
  }
});
// the sortedIndices array is now sorted
// the first element in the array is the index of the name that is least different from nameIn
// the last element in the array is the index of the name that is most different from nameIn

Levenshtein距离法:

public static int levenshteinDistance(String s, String t) {
  // degenerate cases
  if (s == t) {
    return 0;
  }
  if (s.length() == 0) {
    return t.length();
  }
  if (t.length() == 0) {
    return s.length();
  }

  // create two work vectors of integer distances
  int[] v0 = new int[t.length() + 1];
  int[] v1 = new int[t.length() + 1];

  // initialize v0 (the previous row of distances)
  // this row is A[0][i]: edit distance for an empty s
  // the distance is just the number of characters to delete from t
  for (int i = 0; i < v0.length; i++) {
    v0[i] = i;
  }

  for (int i = 0; i < s.length(); i++) {
    // calculate v1 (current row distances) from the previous row v0

    // first element of v1 is A[i+1][0]
    // edit distance is delete (i+1) chars from s to match empty t
    v1[0] = i + 1;

    // use formula to fill in the rest of the row
    for (int j = 0; j < t.length(); j++) {
      int cost = (s.charAt(i) == t.charAt(j)) ? 0 : 1;
      v1[j + 1] = Math.min(v1[j] + 1, Math.min(v0[j + 1] + 1, v0[j] + cost));
    }

    // copy v1 (current row) to v0 (previous row) for next iteration
    for (int j = 0; j < v0.length; j++) {
      v0[j] = v1[j];
    }
  }

  return v1[t.length()];
}