从字符串创建数组

迈耶

我想创建一个数组,例如整数值。在下面的代码中,我创建了一个 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,因为它仍然存在。

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

非常感谢 !

YCF_L

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

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

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

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

这可以解决您的重复问题,并且您可以使用任意大小的 id,而不仅仅是限制 10

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章