简体   繁体   English

将字符串存储到int []数组中

[英]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.

相关问题 将String数组转换为int值并将其存储在int数组中 - Convert String array to int values and store it in int array 将 String 和 Int 存储在数组中作为 Java 中的输入 - Store String and Int in an Array as input in Java 是否可以将 String 数组和 int 数组存储到 Java 中的数组数组中? - Is it possible to store String array & int array to an array of arrays in Java? 为字符串中的单词分配唯一的 int 值,然后将其存储到数组中 - Assign unique int values to a word in a String then store it into an Array 将字符串存储在int中 - store a string in an int 将数据文件(包含字符串和int类型)存储到两个数组(string数组,int数组)中 - store data file (that holds strings and int types) into two arrays (string array, int array) Java:将Int存储在Integer数组中 - Java: Store int in Integer array 如何将由空格缩进的用户输入存储到 String 数组中并将数字转换为 int 数组? - How do I store the user input that is indented by a space into the String array and convert the number into int array? Android / Java存储来自字符串数组中文本文件的名称,以及位于int数组中的整数 - Android/Java Store names from a text file in String array, and integers in int array 字符串到数组的整数 - String to Array Int
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM