java - 从字符串创建数组

标签 java arrays string

我想创建一个数组,例如整数值。在我下面的代码中,我创建了一个 while 循环,在其中我得到了一个包含很多信息的长字符串,但现在我只需要这个字符串中的 1 个数字值,我用子字符串方法得到这个数字。

如何将这个子字符串填充到一个数组中。每次在 while 循环中,我都会从我的文件中得到一个新行,字符串在哪里。而且每次的数量都不一样。

这是我的代码:

public static void main(String[] args) {
         Text m;
          String a;
         Set<Integer> set = new HashSet<>();


          try {
             m = new Text();
             while(m.hasLine) {
                a = m.nextline(); // Here I get the string which looks like :  <name id="654" time="123"  number=”345”  value=”789” />


                String z2 = a.substring(10,13);     // Here I get String : 654
                String z3 = a.substring(21,24);     // String: 123
                String z4 = a.substring(35,38);     // String: 345
                String z5 = a.substring(48,51);     // String: 789
                String z6 = a.substring(60,64);     // String: 4326

                Integer i2 = Integer.valueOf(z2);   //Convert all to integer values
                Integer i3 = Integer.valueOf(z3);
                Integer i4 = Integer.valueOf(z4);
                Integer i5 = Integer.valueOf(z5);
                Integer i6 = Integer.valueOf(z6); 



                //System.out.println(z2);


                String r1;
                if(set.contains(i2)){                     // If The ID is in my array don't set it in the array only create string r1 with MEssage:2

                     r1 = "{\"Message\":2,";
                   }
                    else{
                         set.add(i2);                     // If the ID is not in my arrray set it in my array and create string r2 with Message: 1
                         r1 = "{\"Message\":1,";
                    }

               // 1. How can I  expand this array for the values  of time; number; and value . So if the ID Value isn't in the array put it in there with the 4 values. and create String with Message: 2
               // 2. And if the ID is in there check if value time is the same , if not , create String with Message 2
               // 3. And if the ID is in there check if value time is not the same, then check if value number and value is the same ... if yes Create string Message 3: for example

                // So how can I do such a thing. where I can create a 4 dimensional array and check if the values so in there ???





             String s = r1;

                System.out.println(s);

             }
          }catch (FileNotFoundException e) {
                 System.out.println("File not found!");
    }
}
}

所以我想做的主要事情是在我的 while 循环中我每次都从我的文档中获取下一行。线对线!我得到的每一行都是一个字符串,每个字符串中都有一个 ID 号。现在我只想用这个 id 号创建一个数组。 我想这样做的原因是,我有一个很大的文档,可能是一个 id 在那里多次出现,将来我希望每一行都首先检查这个 id 号是否在我的数组中以及它是否存在不应该在我的数组中填充这个 id,因为它仍然存在。

我希望你明白我想做什么!

非常感谢!

最佳答案

就像@XtremeBaumer 在评论中建议您可以使用一组整数,因此您可以使用 String[] myArr = new String[10]; 而不是:

Set<Integer> set = new HashSet<>();

然后就可以在这个Set里面填id了

set.add(Integer.parseInt(z2));

这可以解决你的重复问题,你可以使用任何大小的 id,而不是限制 10

关于java - 从字符串创建数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44022493/

相关文章:

java - 如何从给定的两个字符串数组形成第三个java数组

c - 为什么当 N 越界时 &a[N] 不调用 UB?

c - wchar_t 与 wint_t

java - 评论是否从最终 apk 中删除?

java - 在具有 2 种不同布局的 Android Listview 中重用 View

java - 为什么即使对于不相等的字符串,equalsignorecase 也会返回 true

ruby - 按特定条件分割的字符串

java - .equals ("0") 和 .equals ('0' ) 之间的区别

java - 适用于多个 fatJars 的 Gradle

java - 我无法限制 Java 堆大小