Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9d1a503

Browse files
author
jsquared21
committed
Add Ex_27.09
1 parent73ddcf1 commit9d1a503

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*********************************************************************************
2+
* (Implement hashCode for string) Write a method that returns a hash code for *
3+
* string using the approach described in Section 27.3.2 with b value 31. The *
4+
* function header is as follows: *
5+
* *
6+
* public static int hashCodeForString(String s) *
7+
*********************************************************************************/
8+
public class Exercise_27_09 {
9+
public static void main(String[] args) {
10+
11+
}
12+
}
1.09 KB
Binary file not shown.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*********************************************************************************
2+
* (Implement hashCode for string) Write a method that returns a hash code for *
3+
* string using the approach described in Section 27.3.2 with b value 31. The *
4+
* function header is as follows: *
5+
* *
6+
* public static int hashCodeForString(String s) *
7+
*********************************************************************************/
8+
publicclassExercise_27_09 {
9+
publicstaticvoidmain(String[]args) {
10+
// Create a list if string
11+
String[]list = {"Mark","Smith","tops","pots","tod","dot"};
12+
13+
// Print the hash code for each string
14+
for (Stringe :list) {
15+
System.out.println("The hash code for\"" +e +
16+
"\" is " +hashCodeForString(e));
17+
}
18+
19+
}
20+
21+
/** Retrun the hash code for string */
22+
publicstaticinthashCodeForString(Strings) {
23+
intb =31;
24+
inthashCode =0;
25+
for (inti =0;i <s.length();i++) {
26+
hashCode =b *hashCode + (int)s.charAt(i);
27+
}
28+
returnhashCode;
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp