Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中尝试查找数组中字符串的索引_Java_Arrays_String_Indexof - Fatal编程技术网

在java中尝试查找数组中字符串的索引

在java中尝试查找数组中字符串的索引,java,arrays,string,indexof,Java,Arrays,String,Indexof,我试图找到数组中最长字符串的索引,它由字符串longString表示。我一直收到一个“找不到符号”错误,不知道如何修复它 public class Final21 { public static String getLongString(String[] array) { int x=0; int maxLength = 0; String longString = null; for (String

我试图找到数组中最长字符串的索引,它由字符串longString表示。我一直收到一个“找不到符号”错误,不知道如何修复它

public class Final21 {
       public static String getLongString(String[] array) {
          int x=0;
          int maxLength = 0;
          String longString = null;
          for (String s : array) {
              if (s.length() > maxLength) {
                  maxLength = s.length();
                  longString = s;
              }
          }
          return longString;
      }

         public static String getShortString(String[] array) {
          int minLength = 0;
          String shortString = null;
          for (String t : array) {
              if (t.length() > minLength) {
                  minLength = t.length();
                  shortString = t;
              }
          }
          return shortString;
      }

      public static void main(String[] args) {
          String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
             String longString = getLongString(names);
                System.out.println("The Longest String is: " + longString + " With The Index Of" + names.indexOf(longString));

             String shortString = getShortString(names);
                System.out.println("The Longest String is: " + shortString + " With The Index Of" );
      }

    }

问题在于
names.indexOf(longString)
。因为
names
属于
String[]
类型,它是一个数组。此类型
String[]
没有名为
indexOf
的方法定义。你也可以试试

所以,为了纠正代码剪贴,您可以像这样重写它

System.out.println("The Longest String is: " + longString + " With The Index Of" + java.util.Arrays.asList(names).indexOf(longString));
阅读的JavaDoc,学习如何使用javaapi在java中处理数组


此外,可以通过修改代码的语义来实现同样的效果。我是为你做的。也请阅读这篇文章。

数组中没有
indexOf
函数,而不是您当前的方法-我将从
getLongString
getShortString
方法返回
索引
;首先假设它是第一个元素。如果任何元素更长(或更短),则更新返回值。像

public static int getLongString(String[] array) {
    int max = 0;
    for (int i = 1; i < array.length; i++) {
        if (array[i].length() > array[max].length()) {
            max = i;
        }
    }
    return max;
}

public static int getShortString(String[] array) {
    int min = 0;
    for (int i = 1; i < array.length; i++) {
        if (array[i].length() < array[min].length()) {
            min = i;
        }
    }
    return min;
}
哪个输出

The Longest String is: tomtomjack With The Index Of 3
The Longest String is: bob With The Index Of 0
indexOf()的语法为:

公共int索引of(char-ch)

它返回指定字符第一次出现时该字符串内的索引,如果该字符没有出现,则返回-1

以获得所需的输出

您可以尝试使用以下命令更改代码:

public static String getLongString(String[] array) {
     int x=0, maxLength = 0, index = 0;
     String longString = null;

     for (String s : array) {
         if (s.length() > maxLength) 
         {
             maxLength = s.length();
             index++;
             longString = s;
         }
     }
 return "The Longest String is: "+ longString + " With The Index Of :" + index;
}

 public static void main(String[] args) {
       String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
       String longString = getLongString(names);
       System.out.println(longString);

 }

您得到的
找不到符号
错误的行号是多少?请尝试具体说明您得到错误的点。您可以这样做,只需在这里发布编译器输出。如果你这样做,你的问题和答案也会更有成效……)@罗密欧塞拉哦,是的,对不起。。。是在names.indexOf。我是新来的,我尝试了@Romeo Sierra的解决方案,似乎效果不错。虽然您的解决方案也可以工作,但它需要更多的代码行。
public static String getLongString(String[] array) {
     int x=0, maxLength = 0, index = 0;
     String longString = null;

     for (String s : array) {
         if (s.length() > maxLength) 
         {
             maxLength = s.length();
             index++;
             longString = s;
         }
     }
 return "The Longest String is: "+ longString + " With The Index Of :" + index;
}

 public static void main(String[] args) {
       String[] names = {"bob", "maxwell", "charley", "tomtomjack"};
       String longString = getLongString(names);
       System.out.println(longString);

 }