转换字符串数组为int数组 [英] converting string array into int array

查看:199
本文介绍了转换字符串数组为int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止,我需要把这个字符串数组转换成只是一个整数数组,字符串数组看起来像这样

this is what i have so far, i need to convert this string array into just an array of integers, the string array looks something like this

wholef[0] = "2 3 4";
wholef[1] = "1 3 4";
wholef[2] = "5 3 5";
wholef[3] = "4 5 6";
wholef[4] = "3 10 2";

这些值来自我从中读取的文本文件,但现在我需要将其转换成一个大的整数数组,我尝试使用拆分方法,但林不知道是否会在这种设置的工作。如果任何人都可以给我一个更好的办法将是不错,但我只是需要将其转换成整数数组,真的就是所有我需要的。

these values come from a text file that i read from but now i need to convert this into one big array of integers, im trying to use the split method but im not sure if it will work on this kind of setup. if anyone can give me a better way it would be nice but i just need to convert this into an array of integers, thats really all i need.

for(int k = 0; k < fline; k++)
    {
        String[] items = wholef[k].replaceAll(" ", "").split(",");

        int[] parsed = new int[wholef[k].length];

        for (int i = 0; i < wholef[k].length; i++)
        {
            try 
            {
                parsed[i] = Integer.parseInt(wholef[i]);
            } catch (NumberFormatException nfe) {};
        }
    }

这是现在使用,它非常接近的原因,新code IM我只得到一个错误

This is the new code im using now, its very close cause i only get one error

int q = 0;
        for (String crtLine : wholef) 
        {
            int[] parsed = new int[wholef.length];

            String[] items = crtLine.split(" ");
            for (String crtItem: items) 
            {
                parsed[q++] = Integer.parse(crtItem);
            }
        }

误差是这
java的:97:错误:无法找到符号解析[问++} = Integer.parse(crtItem);
                                                        ^
符号:方法解析(字符串)
位置:Integer类
1误差

the error is this java:97: error: cannot find symbol parsed[q++} = Integer.parse(crtItem); ^ symbol: method parse(String) location: class Integer 1 error

推荐答案

这把你的字符串数组,并转储到intwholef [n..total]
如果你想让它成为一个二维数组或对象数组,你必须做一些额外的。然后,你可以做一个对象数组,并让每个组值作为一个属性。

This take your string array and dumps it into intwholef[n..total]; If you want it into a 2D array or an object array you have to do some additional. Then you can do an array of objects, and have each set of values as an attribute.

 String[] parts = wholef[0].split(" ");
 int[] intwholef= new int[parts.length];

 for(int n = 0; n < parts.length; n++) {
    intwholef[n] = Integer.parseInt(parts[n]);
  }

这篇关于转换字符串数组为int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆