[英]Store string into int[] array
I have a string String strings ="100.122.323.344;543.433.872.438;218.544.678.322";
我有一个字符串
String strings ="100.122.323.344;543.433.872.438;218.544.678.322";
I want to store into int[] like this int[] ={{100,122,323,344},{543,433,872,438},{218,544,678,322}}
Below is the sample code 我想像这样存储到int []中
int[] ={{100,122,323,344},{543,433,872,438},{218,544,678,322}}
下面是示例代码
public static void main(String[] args) {
// TODO Auto-generated method stub
String strings = "100.122.323.344;543.433.872.438;218.544.678.322";
strings = strings.replace(".", ",");
System.out.println(strings);
String[] coordinates = strings.split(";");
String[] rect=null;
int[] intcoordinates;
for(int i=0;i<coordinates.length;i++)
{
//System.out.println(coordinates[i]);
rect= coordinates[i].split(",");
for(int j=0;j<rect.length;j++)
{
}
}
}
Till now i am able to separate value from string but don't know how to convert to int please help 到目前为止,我能够将值与字符串分开,但是不知道如何转换为int,请帮助
You may use 您可以使用
int intrect = Integer.parseInt(rect[j]);
to convert your rect[j] to int. 将您的rect [j]转换为int。
public static void main(String[] args) {
// TODO Auto-generated method stub
String strings = "100.122.323.344;543.433.872.438;218.544.678.322";
strings = strings.replace(".", ",");
System.out.println(strings);
String[] coordinates = strings.split(";");
String[] rect = null;
int[][] intcoordinates = new int[3][4]; // initialize the size of the array
for (int i = 0; i < coordinates.length; i++) {
// System.out.println(coordinates[i]);
rect = coordinates[i].split(",");
for (int j = 0; j < rect.length; j++) {
intcoordinates[i][j] = Integer.parseInt(rect[j]);
}
}
}
intcoordinates contains [[100, 122, 323, 344], [543, 433, 872, 438], [218, 544, 678, 322]] at the end of the execution. intcoordinates在执行结束时包含[[100,122,323,344],[543,433,872,438],[218,544,678,322]。 This is the final result of the conversion.
这是转换的最终结果。
First, you want to store the value as int, you should know the {{100,122,323,344},{543,433,872,438},{218,544,678,322}}
is a two dimensional array, so it's int[][]
. 首先,您要将值存储为int,应该知道
{{100,122,323,344},{543,433,872,438},{218,544,678,322}}
是一个二维数组,所以它是int[][]
。 Then the array must be init, the row and col can be figured out . 然后,该数组必须为init,可以找出该行和col。
int[][] intcoordinates = new int[3][4]; // init the array, the row and the col can be figured.
for(int i=0; i<coordinates.length; i++)
{
//System.out.println(coordinates[i]);
rect = coordinates[i].split(",");
for(int j=0; j<rect.length; j++)
{
intcoordinates[i][j] = Integer.parseInt(rect[j]);
}
}
System.out.println(Arrays.deepToString(intcoordinates));
The final execution is [[100, 122, 323, 344], [543, 433, 872, 438], [218, 544, 678, 322]]
. 最终执行为
[[100, 122, 323, 344], [543, 433, 872, 438], [218, 544, 678, 322]]
。 To get the output {{100,122,323,344},{543,433,872,438},{218,544,678,322}}
, you must make some additional methods. 要获得输出
{{100,122,323,344},{543,433,872,438},{218,544,678,322}}
,您必须制作一些其他方法。 So if you just want to get the output, i'd say just replace the .
因此,如果您只是想获取输出,我会说只需替换一下
.
to ,
, ;
对
,
, ;
to },{
, and add the curly braces. 到
},{
,并添加花括号。
Initialize a two dimentional array intcoordinates
with rows and cols. 使用行和列初始化两个二维数组
intcoordinates
。 If don't know the row size, than you should count maximum row size. 如果不知道行大小,则应该计算最大行大小。 Than convert
String
to int
and assign to this array. 比将
String
转换为int
并分配给此数组。
int[][] intcoordinates=new int[coordinates.length][countMaxRow(coordinates)];
for(int i=0;i<coordinates.length;i++){
rect= coordinates[i].split(",");
for(int j=0;j<rect.length;j++){
intcoordinates[i][j]=Integer.parseInt(rect[j]);
}
}
System.out.println(Arrays.deepToString(intcoordinates));
Here is maximum row count method 这是最大行数方法
static int countMaxRow(String[] arr){
int max=0;
for (String str : arr) {
String a[]=str.split(",");
if(max< a.length){
max= a.length;
}
}
return max;
}
Conceptually Scanner and collections should work for you: 从概念上讲,扫描仪和集合应为您工作:
String strings = "100.122.323.344;543.433.872.438;218.544.678.322";
Scanner scanner = new Scanner(new ByteArrayInputStream(strings.getBytes()));
scanner.useDelimiter(";");
List<List<Integer>> list = new ArrayList<>();
while(scanner.hasNext())
{
Scanner scanner2 = new Scanner(new ByteArrayInputStream(scanner.next().getBytes()));
scanner2.useDelimiter("\\D");
List<Integer> lst = new ArrayList<>();
while(scanner2.hasNextInt())
lst.add(scanner2.nextInt());
list.add(lst);
}
System.out.println("Lists: ");
for(List<Integer> lst : list)
System.out.println(lst);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.