将唯一的 int 值分配给字符串中的单词,然后将其存储到数组中
Assign unique int values to a word in a String then store it into an Array
我正在尝试获取字符串中的唯一单词,然后将其连同出现的次数一起存储在数组中。
我的方法是在 if-else
语句中为每个唯一的字符串分配一个唯一的整数,然后获取这些整数的唯一计数,并从分配了唯一值的字符串中提取任何一个单词,最后取唯一整数出现的次数。
但是,每当我为 String
分配一个唯一值时,我得到 null
:
import java.util.Arrays;
public class StringLearning {
public static void main(String[] args) {
final String text = "Win Win Win Win Draw Draw Loss Loss";
int[] numbers = null;
if(text.equals("Win")) {
numbers= new int[1];
}else if(text.equals("Draw")){
numbers = new int [2];
}else if(text.equals("Loss")) {
numbers = new int [3];
}
System.out.println(Arrays.toString(numbers));
}
}
预期输出:
{1, 1, 1, 1, 2, 2, 3, 3}
编辑:
这在一般意义上如何实施?这样一来,我不知道字符串中有“Win、Loss、Draw”,但我想为任何给定的唯一单词分配一个唯一整数?
你的代码有很多问题。
首先,您必须单独获取字符串中的每个单词,因此您需要split
将其放入数组中。
其次,您需要创建一个数组,并将整数放入其中。 (你所做的是为每个数字创建新数组)
我试图解决这些问题。希望你能理解我的代码。
(我只是修复了你的代码,并没有让它通用)
String[] arr = text.split(" ");
int[] numbers = new int[arr.length];
for (int i=0;i<arr.length;i++){
if(arr[i].equals("Win")) {
numbers[i] = 1;
}else if(arr[i].equals("Draw")){
numbers[i] = 2;
}else if(arr[i].equals("Loss")) {
numbers[i] = 3;
}
}
创建数组对象时,括号中的数字new int[1]
声明数组的长度。我建议为此使用 HashMap。代码示例为:
public class StringLearning {
public static void main(String[] args) {
final String text = "Win Win Win Win Draw Draw Loss Loss";
HashMap<String, Integer> counts = new HashMap<String, Integer>();
for (String word : text.split(" ")) { // loops through each word of the string
// text.split(" ") returns an array, with all the parts of the string between your regexes
// if current word is not already in the map, add it to the map.
if (!counts.containsKey(word)) counts.put(word, 0);
counts.put(word, counts.get(word) + 1); // adds one to the count of the current word
}
// lambda expression
counts.forEach((string, integer) -> System.out.printf("amount of \"%s\": %d\n", string, integer));
}
}
输出:
amount of "Draw": 2
amount of "Loss": 2
amount of "Win": 4
编辑(对 tQuadrat 的回应):
我编辑了这个 post 以保持所有内容清晰易读。
public class StringLearning {
public static void main(String[] args) {
final String text = "Win Win Win Win Draw Draw Loss Loss";
HashMap<String, Integer> counts = new HashMap<String, Integer>();
for (String word : text.split(" ")) { // loops through each word of the string
// text.split(" ") returns an array, with all the parts of the string between your regexes
// if current word is not already in the map, add it to the map.
if (!counts.containsKey(word))
counts.put(word, counts.size() + 1); // index of the word (+ 1 because your expected output was 1-indexed)
}
for (String word : text.split(" ")) {
System.out.print(counts.get(word) + " ");
}
}
}
输出:
1 1 1 1 2 2 3 3
让我们试试这个方法:
获取字符串中的单词数:var words = text.split( " " );
这假设单词仅由空格分隔。
为单词创建注册表:Map<String,Integer> registry = new HashMap<>();
创建结果数组:var result = new int [words.length];
现在遍历单词并检查您是否已经看到单词:
for( var i = 0; i < words.length; ++i )
{
result [i] = registry.computeIfAbsent( words [i], $ -> registry.size() + 1 );
}
最后,打印结果:System.out.println( Arrays.toString( result ) );
——尽管这将使用[…]
而不是{…}
。
registry.computeIfAbsent( words [i], $ -> registry.size() + 1 );
为每个未见过的单词在注册表中添加一个新条目,并分配注册表的大小加一作为该单词的唯一编号。对于已知单词,它 returns 之前分配的数字。
这适用于 text
中的任何一组单词。
所以完整的东西可能是这样的:
public final class StringLearning
{
public static final void main( final String... args )
{
final var text = "Win Win Win Win Draw Draw Loss Loss"; // … or any other text
final var words = text.split( " " );
final Map<String,Integer> registry = new HashMap<>();
final var result = new int [words.length];
for( var i = 0; i < words.length; ++i )
{
result [i] = registry.computeIfAbsent( words [i], $ -> registry.size() + 1 );
}
System.out.println( Arrays.toString( result ) );
}
}
在映射中为字符串的每个单词维护唯一值,然后在分配数组时获取唯一值。
String text = "Win Win Loss Draw Hello";
String[] split = text.split(" ");
// To maintain unique value for each word of input string
Map<String, Integer> = new HashMap<>();
int counter = 0;
for(String ele:split)
if(!map.containsKey(ele))
map.put(ele, ++counter)
// Getting unique value for each word and assigining in array
int[] array=new int[split.length];
for(int i=0; i<split.length;i++)
array[i] = map.get(split[i]);
我正在尝试获取字符串中的唯一单词,然后将其连同出现的次数一起存储在数组中。
我的方法是在 if-else
语句中为每个唯一的字符串分配一个唯一的整数,然后获取这些整数的唯一计数,并从分配了唯一值的字符串中提取任何一个单词,最后取唯一整数出现的次数。
但是,每当我为 String
分配一个唯一值时,我得到 null
:
import java.util.Arrays;
public class StringLearning {
public static void main(String[] args) {
final String text = "Win Win Win Win Draw Draw Loss Loss";
int[] numbers = null;
if(text.equals("Win")) {
numbers= new int[1];
}else if(text.equals("Draw")){
numbers = new int [2];
}else if(text.equals("Loss")) {
numbers = new int [3];
}
System.out.println(Arrays.toString(numbers));
}
}
预期输出:
{1, 1, 1, 1, 2, 2, 3, 3}
编辑: 这在一般意义上如何实施?这样一来,我不知道字符串中有“Win、Loss、Draw”,但我想为任何给定的唯一单词分配一个唯一整数?
你的代码有很多问题。
首先,您必须单独获取字符串中的每个单词,因此您需要split
将其放入数组中。
其次,您需要创建一个数组,并将整数放入其中。 (你所做的是为每个数字创建新数组)
我试图解决这些问题。希望你能理解我的代码。 (我只是修复了你的代码,并没有让它通用)
String[] arr = text.split(" ");
int[] numbers = new int[arr.length];
for (int i=0;i<arr.length;i++){
if(arr[i].equals("Win")) {
numbers[i] = 1;
}else if(arr[i].equals("Draw")){
numbers[i] = 2;
}else if(arr[i].equals("Loss")) {
numbers[i] = 3;
}
}
创建数组对象时,括号中的数字new int[1]
声明数组的长度。我建议为此使用 HashMap。代码示例为:
public class StringLearning {
public static void main(String[] args) {
final String text = "Win Win Win Win Draw Draw Loss Loss";
HashMap<String, Integer> counts = new HashMap<String, Integer>();
for (String word : text.split(" ")) { // loops through each word of the string
// text.split(" ") returns an array, with all the parts of the string between your regexes
// if current word is not already in the map, add it to the map.
if (!counts.containsKey(word)) counts.put(word, 0);
counts.put(word, counts.get(word) + 1); // adds one to the count of the current word
}
// lambda expression
counts.forEach((string, integer) -> System.out.printf("amount of \"%s\": %d\n", string, integer));
}
}
输出:
amount of "Draw": 2
amount of "Loss": 2
amount of "Win": 4
编辑(对 tQuadrat 的回应): 我编辑了这个 post 以保持所有内容清晰易读。
public class StringLearning {
public static void main(String[] args) {
final String text = "Win Win Win Win Draw Draw Loss Loss";
HashMap<String, Integer> counts = new HashMap<String, Integer>();
for (String word : text.split(" ")) { // loops through each word of the string
// text.split(" ") returns an array, with all the parts of the string between your regexes
// if current word is not already in the map, add it to the map.
if (!counts.containsKey(word))
counts.put(word, counts.size() + 1); // index of the word (+ 1 because your expected output was 1-indexed)
}
for (String word : text.split(" ")) {
System.out.print(counts.get(word) + " ");
}
}
}
输出:
1 1 1 1 2 2 3 3
让我们试试这个方法:
获取字符串中的单词数:
var words = text.split( " " );
这假设单词仅由空格分隔。为单词创建注册表:
Map<String,Integer> registry = new HashMap<>();
创建结果数组:
var result = new int [words.length];
现在遍历单词并检查您是否已经看到单词:
for( var i = 0; i < words.length; ++i ) { result [i] = registry.computeIfAbsent( words [i], $ -> registry.size() + 1 ); }
最后,打印结果:
System.out.println( Arrays.toString( result ) );
——尽管这将使用[…]
而不是{…}
。
registry.computeIfAbsent( words [i], $ -> registry.size() + 1 );
为每个未见过的单词在注册表中添加一个新条目,并分配注册表的大小加一作为该单词的唯一编号。对于已知单词,它 returns 之前分配的数字。
这适用于 text
中的任何一组单词。
所以完整的东西可能是这样的:
public final class StringLearning
{
public static final void main( final String... args )
{
final var text = "Win Win Win Win Draw Draw Loss Loss"; // … or any other text
final var words = text.split( " " );
final Map<String,Integer> registry = new HashMap<>();
final var result = new int [words.length];
for( var i = 0; i < words.length; ++i )
{
result [i] = registry.computeIfAbsent( words [i], $ -> registry.size() + 1 );
}
System.out.println( Arrays.toString( result ) );
}
}
在映射中为字符串的每个单词维护唯一值,然后在分配数组时获取唯一值。
String text = "Win Win Loss Draw Hello";
String[] split = text.split(" ");
// To maintain unique value for each word of input string
Map<String, Integer> = new HashMap<>();
int counter = 0;
for(String ele:split)
if(!map.containsKey(ele))
map.put(ele, ++counter)
// Getting unique value for each word and assigining in array
int[] array=new int[split.length];
for(int i=0; i<split.length;i++)
array[i] = map.get(split[i]);