Search string array without a using a loop

↘锁芯ラ 提交于 2019-12-23 15:44:45

问题


I have the following array

String[] arrKey  = new String[] {"A","B","C","D","E",......"Y","Z"};

I would like to search the array and give me the index of where the letter is. Say for example I want to search for the letter "E" and when I search the array it should give me the position of "E" so I should get index position 4. I don't want to do this in a loop. Is it possible? I have been looking all around and can't find an answer.


回答1:


I don't want to do this in a loop.

There has to be a loop somewhere - either in your code or library code.

So yes, you can use

int index = Arrays.asList(arrKey).indexOf("E");

... but that will loop under the covers.

If you know that your array is sorted to start with, you can use:

int index = Arrays.binarySearch(arrKey, "E");

That will be more efficient - but it's still a loop...

Of course, if you know that your array is always A-Z, then you can do it without a loop - but I assume your real case is more generalized...




回答2:


If you have to deal with chars and not strings have a look at the getNumericValue method.

If you want a more general solution you should consider using a Map<String,Integer> rather than an array.




回答3:


It is basically a interview question, to test your recursion skills, here is how you do it.

private static <T> int findIndex(T[] items, T item, int index) {
    if (items.length == index) {
        return -1;
    }
    if (items[index].equals(item)) {
        return index;
    }
    return findIndex(items, item, index+1);
}

Here is how you can run it

int val = findIndex(new String[]{"A","B","C"}, "C", 0);



回答4:


Use indexOf

Java - get element position in array




回答5:


Use indexOf

   return arrKey.get(arrKey.indexOf("E"));



回答6:


It only possible if you assume you have this array (or one similar) as you can calculate the index.

String s = "E";
int index = s.charAt(0) - 'E'; // == 4



回答7:


Here is the code I am working on. This is what I have at the moment. It is just a practice round for me to practice JAVA and understand the Ceaser Cipher.

public class CeaserCipher
{
public static void main (String [] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter test letters for Caesar cipher in capitals");
    String input = keyboard.nextLine();
    char[] strArray = input.toCharArray();

    System.out.print("What is the key: "); 
    int key = keyboard.nextInt();
    //String[] arrKey  = new String[] {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
    String c  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char[] arrKey = c.toCharArray();

    for (int i = 0; i < strArray.length ; i++){

        char cipherValue = strArray[i];
        int index = Arrays.binarySearch(arrKey, cipherValue);
        int j = (key + index)%26;
        System.out.print(arrKey[j]);

    }
}
}


来源:https://stackoverflow.com/questions/13291635/search-string-array-without-a-using-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!