我无法获得任何 indexOf() 函数来处理我编写的这段代码
I can't get any indexOf() function to work for this code that I wrote
package Captain_Ship.alphanum;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
public class A2N {
public static String main(String input) {
char[] alphabet = {'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 output = "";
char[] input_char = input.toLowerCase().toCharArray();
int[] output_int = {};
//Still doesn't work. outputs [I@123772c4 in place of 8 5 12 12 15
for (int i = 0; i>input_char.length; i++) {
output_int[i] = ArrayUtils.indexOf(alphabet,input_char[i]);
}
output = Arrays.toString(output_int);
return output;
}
}
这是我的代码。它的目标非常简单。拿一个句子,把每个字母翻译成一个数字。所以 A 将是 1,B 将是 2,等等。我已经尝试了所有方法来尝试让循环按我想要的方式工作。这段代码是目前唯一给了我一些与此类似的东西的版本:[I@123772c4.在我研究过的选项中,我有 运行 个。
- 如果您不知道数组的大小,请使用
List
而不是数组,例如您不知道 output_int
的大小,因此最好使用动态数组类型的对象,例如List
.
- 您也可以使用
List
来存储字符。 List
是 Java 集合 API 的一部分,这样您就不必使用像 org.apache.commons.lang3.ArrayUtils
这样的第三方库来进行查找元素索引等操作。
- 你的循环应该检查
i < input_char.length
而不是 i > input_char.length
,后者永远不会是 true
.
演示:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// List of alphabets
List<Character> alphabet = List.of('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');
// A sample string
String input = "sample string";
char[] input_char = input.toLowerCase().toCharArray();
// List to store the index of each character of `input` in `alphabet`. Note: if
// a character is not found, the indexOf method returns -1
List<Integer> output_int = new ArrayList<>();
for (int i = 0; i < input_char.length; i++) {
output_int.add(alphabet.indexOf(input_char[i]));
}
// Display the list
System.out.println(output_int);
}
}
输出:
[18, 0, 12, 15, 11, 4, -1, 18, 19, 17, 8, 13, 6]
for (int i = 0; i>input_char.length; i++) {
永远不会执行,因为 i
在初始化为 0
.
时永远不会大于 input_char.length
所以改成for (int i = 0; i < input_char.length; i++) {
然后将output_int
初始化为长度等于input_char.length
的char数组。
int[] output_int = new int[input_char.length]
.
不要命名您的方法 main
,尽管它可以编译 - 绝不可取。 main
用作您的应用程序的入口点。
indexOf()
方法returns给定字符值或子字符串的索引。如果没有找到,则 returns -1
.
public class Main {
public static void main(String[] args) {
char[] alphabet = {'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'};
Map<Integer, String> map = new HashMap<>();
map = alphabetFrom0Index(alphabet);
// printIndexAndLetters(alphabetFrom0Index(alphabet));
String test = "Does it work";
String test2 = "abc xyz";
System.out.println(returnIndexNumbers(map, test)); // 15 5 19 9 20 23 15 18 11
System.out.println(returnIndexNumbers(map, test2)); // 1 2 3 24 25 26
}
// return a string whit the index o letter from the alphabet from a map
public static String returnIndexNumbers(Map<Integer, String> map, String test) {
// we create a stringBuilder for not creating every time a string
StringBuilder result = new StringBuilder();
test.toLowerCase();
// we iterate the string
for (int i = 0; i < test.length(); i++) {
// save the letter into a string from a char
String letter = String.valueOf(test.charAt(i));
// iterate over map, and if it match save the index to string
for (Map.Entry<Integer, String> entry : map.entrySet()) {
if (letter.equals(entry.getValue())) {
result.append(entry.getKey()+ " ");
}
}
}
// convert stringBuilder to a string. Wrapper
return String.valueOf(result);
}
// for every character from the alphabet starting from 0
// we give them an index (0 - 25)
public static Map<Integer, String> alphabetFrom0Index(char[] alphabet) {
// we defined a map that contains a pair of key-value
// key is the index of the character & value is the character itself
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < alphabet.length; i++) {
// i is the position of the character from the array
// and i+1 to start from 1, and not from 0
map.put(i+1, String.valueOf(alphabet[i]));
}
return map;
}
// print index and letter from a map
public static void printIndexAndLetters(Map<Integer, String> map) {
map.entrySet().stream().forEach((Map.Entry<Integer, String> entry) -> {
System.out.println(entry.getKey() + " " + entry.getValue());
});
}
}
package Captain_Ship.alphanum;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Arrays;
public class A2N {
public static String main(String input) {
char[] alphabet = {'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 output = "";
char[] input_char = input.toLowerCase().toCharArray();
int[] output_int = {};
//Still doesn't work. outputs [I@123772c4 in place of 8 5 12 12 15
for (int i = 0; i>input_char.length; i++) {
output_int[i] = ArrayUtils.indexOf(alphabet,input_char[i]);
}
output = Arrays.toString(output_int);
return output;
}
}
这是我的代码。它的目标非常简单。拿一个句子,把每个字母翻译成一个数字。所以 A 将是 1,B 将是 2,等等。我已经尝试了所有方法来尝试让循环按我想要的方式工作。这段代码是目前唯一给了我一些与此类似的东西的版本:[I@123772c4.在我研究过的选项中,我有 运行 个。
- 如果您不知道数组的大小,请使用
List
而不是数组,例如您不知道output_int
的大小,因此最好使用动态数组类型的对象,例如List
. - 您也可以使用
List
来存储字符。List
是 Java 集合 API 的一部分,这样您就不必使用像org.apache.commons.lang3.ArrayUtils
这样的第三方库来进行查找元素索引等操作。 - 你的循环应该检查
i < input_char.length
而不是i > input_char.length
,后者永远不会是true
.
演示:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// List of alphabets
List<Character> alphabet = List.of('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');
// A sample string
String input = "sample string";
char[] input_char = input.toLowerCase().toCharArray();
// List to store the index of each character of `input` in `alphabet`. Note: if
// a character is not found, the indexOf method returns -1
List<Integer> output_int = new ArrayList<>();
for (int i = 0; i < input_char.length; i++) {
output_int.add(alphabet.indexOf(input_char[i]));
}
// Display the list
System.out.println(output_int);
}
}
输出:
[18, 0, 12, 15, 11, 4, -1, 18, 19, 17, 8, 13, 6]
for (int i = 0; i>input_char.length; i++) {
永远不会执行,因为 i
在初始化为 0
.
input_char.length
所以改成for (int i = 0; i < input_char.length; i++) {
然后将output_int
初始化为长度等于input_char.length
的char数组。
int[] output_int = new int[input_char.length]
.
不要命名您的方法 main
,尽管它可以编译 - 绝不可取。 main
用作您的应用程序的入口点。
indexOf()
方法returns给定字符值或子字符串的索引。如果没有找到,则 returns -1
.
public class Main {
public static void main(String[] args) {
char[] alphabet = {'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'};
Map<Integer, String> map = new HashMap<>();
map = alphabetFrom0Index(alphabet);
// printIndexAndLetters(alphabetFrom0Index(alphabet));
String test = "Does it work";
String test2 = "abc xyz";
System.out.println(returnIndexNumbers(map, test)); // 15 5 19 9 20 23 15 18 11
System.out.println(returnIndexNumbers(map, test2)); // 1 2 3 24 25 26
}
// return a string whit the index o letter from the alphabet from a map
public static String returnIndexNumbers(Map<Integer, String> map, String test) {
// we create a stringBuilder for not creating every time a string
StringBuilder result = new StringBuilder();
test.toLowerCase();
// we iterate the string
for (int i = 0; i < test.length(); i++) {
// save the letter into a string from a char
String letter = String.valueOf(test.charAt(i));
// iterate over map, and if it match save the index to string
for (Map.Entry<Integer, String> entry : map.entrySet()) {
if (letter.equals(entry.getValue())) {
result.append(entry.getKey()+ " ");
}
}
}
// convert stringBuilder to a string. Wrapper
return String.valueOf(result);
}
// for every character from the alphabet starting from 0
// we give them an index (0 - 25)
public static Map<Integer, String> alphabetFrom0Index(char[] alphabet) {
// we defined a map that contains a pair of key-value
// key is the index of the character & value is the character itself
Map<Integer, String> map = new HashMap<>();
for (int i = 0; i < alphabet.length; i++) {
// i is the position of the character from the array
// and i+1 to start from 1, and not from 0
map.put(i+1, String.valueOf(alphabet[i]));
}
return map;
}
// print index and letter from a map
public static void printIndexAndLetters(Map<Integer, String> map) {
map.entrySet().stream().forEach((Map.Entry<Integer, String> entry) -> {
System.out.println(entry.getKey() + " " + entry.getValue());
});
}
}