从字符串创建数组

时间:2017-05-17 10:37:15

标签: java arrays string

我想创建一个带有例如整数值的数组。在下面的代码中,我创建了一个while循环,其中我获得了一个包含大量信息的长字符串,但现在我只需要来自此String的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,因为它仍然存在。

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

非常感谢你!

3 个答案:

答案 0 :(得分:1)

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

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

然后你可以填写这个集合中的id

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

这可以解决您的重复问题,您可以使用任意大小的ID,而不仅限于10

答案 1 :(得分:0)

此代码将子字符串从10转换为13为整数。

char one = z2.charAt(10);
char two = z2.charAt(11)
char three = z2.charAt(12)
char four = z2.charAt(13);

int num = (((int)one - 48)*1000) + (((int)two - 48)*100) + (((int)three - 48)*10) + ((int)one - 48);

答案 2 :(得分:0)

  public static void main (String[] args) {
        ArrayList<String> inputData = new ArrayList<String>();
        try {
            Scanner in = new Scanner(new BufferedReader(new FileReader("input.txt")));
            while(in.hasNextLine())
                inputData.add(in.nextLine());
            Set<String> retVal = processData(inputData);
        } catch (IOException e) {
            System.out.println("IO error in input.txt or output.txt");
        }
    }

   public static Set<String> processData(ArrayList<String> array) {
    Set<String> set = new HashSet<String>();
    List<String> nums = new ArrayList<String>();
    for(String str : array){
        String sa = str.substring(10, 13);
        nums.add(sa);
        set.addAll(nums);
    }

    return set;
}

这应该有效,retVal将为您提供字符串中的所有唯一编号。