简体   繁体   中英

Casting an array of chars to ints on a hashtable

This is a question regarding casting of types in Java.

public int hashFunction(String D){
    char[] Thing = D.toCharArray();
    for(int i=0; i < Thing.length; i++){
        index =+(int)Thing.length;
    }
    return index % tablesize;
}

Now how does the code work such that each content of the char array is now cast to a type int ?

Java will allow you to assign char s to int s, since int has a larger domain than char . This is known as widening :

char c = 'a';
int i = c; // compiles just fine

You probably want to access each element in Thing , right? Use an enhanced for loop:

for(char c : Thing) {
  // do something with c
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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