Java将字符串数组中列中的元素复制到新的整型数组中

huangapple go评论60阅读模式
英文:

Java copy elements in column from String array to new int array

问题

以下是翻译好的部分:

我只是需要一点关于某事的帮助,似乎无法正确解决。

我有以下代码,它显示了CSV文件中的值。这部分代码是正常工作的。

while(file.hasNext()){
    String data = file.nextLine();
    String[] values = data.split(",");
    for(String index : values){
        System.out.printf("%s \t", index);
    } 
    System.out.println();
}

这段代码显示:

1    John Smith    Engineering
2    Jim Jones     Cooking

我想从 values[0] 中获取值(即ID - 1,2),并将它们复制到一个新的整数数组中,以便我可以将其传递给其他方法来执行搜索等操作。任何帮助将不胜感激。

谢谢!

英文:

just after a bit of help with something that I cant seem to get right.

I have the following code which displays the values in a csv file. This is working.

while(file.hasNext()){
    String data = file.nextLine();
    String[] values = data.split(",");
    for(String index : values){
        System.out.printf("%s \t", index);
    } 
    System.out.println();
}

The code displays:

1    John Smith    Engineering
2    Jim Jones     Cooking

I want to take the values just from values[0] (which are the ID's - 1,2) and copy them into a new int array so I can pass this to other methods to perform searches and whatnot. Any help would be greatly appreciated.

Thanks!

答案1

得分: 0

// 仅翻译代码部分
将 values[0] 保存到一个单独的列表中并将其转换为数组如果您知道 id 的数量可以直接将 id 保存到一个数组中

List<String> ids = new ArrayList<>();
while (file.hasNext()) {
    String data = file.nextLine();
    String[] values = data.split(",");
    if (values.length >= 1) {
        ids.add(values[0]);
        
        for (String index : values) {
            System.out.printf("%s \t", index);
        }
    }
    System.out.println();
}

String[] idsArray = new String[ids.size()];
ids = ids.toArray(idsArray);
// 使用 idsArray
英文:

Just save values[0] into a seperate list and convert it to an array. If you know the number of ids you can directly save your ids into an array.

List&lt;String&gt; ids = new ArrayList&lt;&gt;();
while (file.hasNext()) {
    String data = file.nextLine();
    String[] values = data.split(&quot;,&quot;);
    if (values.length &gt;= 1) {
        ids.add(values[0]);
        
        for (String index : values) {
            System.out.printf(&quot;%s \t&quot;, index);
        }
    }
    System.out.println();
}

String[] idsArray = new Integer[ids.size()];
ids = ids.toArray(idsArray);
// use idsArray

答案2

得分: 0

不太确定我完全理解你的问题,但你可以尝试这样做:

String[] str_array = {...};
int[] int_array = {...};

int_array[index] = Integer.parseInt(str_array[index]);

这样你就可以将一个字符串添加到整数数组中,注意处理NumberFormatException。

编辑:

ArrayList<Integer> ids = new ArrayList<>(); // IDs: 1, 43, 23...
Scanner scanner = new Scanner(System.in); // 从控制台读取
int input = scanner.nextInt();

if (ids.contains(input)) {
  // true
} else {
  // false
}
英文:

Not quite sure I understand your question fully, but you can try this:

String[] str_array = {...};
int[] int_array = {...};

int_array[index] = Integer.parseInt(str_array[index]);

This way you can add a String to an int array, watch out for NumberFormatExceptions.

Edit:

ArrayList&lt;Integer&gt; ids = new ArrayList&lt;&gt;(); // IDs: 1, 43, 23...
Scanner scanner = new Scanner(System.in); // Reading from console
int input = scanner.nextInt();

if (ids.contains(input)) {
  // true
} else {
  // false
}

答案3

得分: 0

可以尝试这样做:)

Map<Integer, List<String>> map = new HashMap<Integer, List<String>>();
while(file.hasNext()){
    String data = file.nextLine();
    String[] values = data.split(",");
    for(int key=0;key<values.size();key++){
        if (!map.containsKey(key)){
            map.put(key, new ArrayList<String>());
        }
        map.get(key).add(values[key]);
    } 
}
Integer[] items = new Integer[map.get(0).size()];
for (i = 0; i < map.get(0).size() ; i++) {
   items[i]=Integer.valueOf(map.get(0).get(i));
}
英文:

you can try this:)

Map&lt;Integer, List&lt;String&gt;&gt; map = new HashMap&lt;Integer, List&lt;String&gt;&gt;();
	while(file.hasNext()){
		String data = file.nextLine();
		String[] values = data.split(&quot;,&quot;);
		for(int key=0;key&lt;values.size();key++){
			if (!map.containsKey(key)){
        		map.put(key, new ArrayList&lt;String&gt;());
			}
			map.get(key).add(values[key]);
		} 
	}
    Integer[] items = new Integer[map.get(0).size()];
    for (i = 0; i &lt; map.get(0).size() ; i++) {
       items[i]=Integer.valueOf(map.get(0).get(i));
    }

huangapple
  • 本文由 发表于 2020年10月12日 16:00:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/64313822.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定