无法将值设置为列表仅替换

时间:2016-04-27 09:43:06

标签: java list

正如您在下面的代码中看到的,myEntries是一个包含字符串数组的列表。它有以下数据。

[TestSoftware,Test Sample,AutoTestSoftware11,1,Concept,Large,java,Medium]

[TestSoftware,Test Sample,AutoTestSoftware12,1,End of Life,Very Large,java,High]

[TestSoftware,Test Sample,AutoTestSoftware13,1,Concept,Large,java,Low]

[TestSoftware,Test Sample,AutoTestSoftware14,1,Concept,Large,java,Critical]

我的目标是,如您所见,我希望从AutoTestSoftware11等字符串中获取数字,并将8添加到该数字。同样,我想将整个String Array推回到myEntries中。我也发布了输出。我面临的问题是,只有最后更改的值反映在myEntries中。

List<String[]> myEntries;
String[] world = new String[8];
int t=0;

for (String[] element:myEntries) {
                k=0;
                for (String temp:element) {

                    if (temp.startsWith("AutoTestSoftware")) {
                        int hi = Integer.parseInt(temp.split("AutoTestSoftware(?=\\d)")[1]);
                        hi += 8;
                        temp = "AutoTestSoftware" + hi;
                    }
                    world[k]=temp;
                    k++;
                }
                System.out.println(Arrays.toString(world)+"value of t: "+t);
                myEntries.set(t,world);
                t++;
            }
            System.out.println("\n\n");
            for (String[] element2 : myEntries)
            {
              System.out.println(Arrays.toString(element2));
            }

输出:

[TestSoftware,Test Sample,AutoTestSoftware19,1,Concept,Large,java,Medium]值t:0

[TestSoftware,Test Sample,AutoTestSoftware20,1,End of Life,Very Large,java,High]值t:1

[TestSoftware,Test Sample,AutoTestSoftware21,1,Concept,Large,java,Low]值t:2

[TestSoftware,Test Sample,AutoTestSoftware22,1,Concept,Large,java,Critical]值t:3

[TestSoftware,Test Sample,AutoTestSoftware22,1,Concept,Large,java,Critical]

[TestSoftware,Test Sample,AutoTestSoftware22,1,Concept,Large,java,Critical]

[TestSoftware,Test Sample,AutoTestSoftware22,1,Concept,Large,java,Critical]

[TestSoftware,Test Sample,AutoTestSoftware22,1,Concept,Large,java,Critical]

1 个答案:

答案 0 :(得分:0)

要解释我的评论,这有效:

List<String[]> myEntries;

int t=0;

for (String[] element:myEntries) {
                k=0;
                String[] world = new String[8];
                for (String temp:element) {

                    if (temp.startsWith("AutoTestSoftware")) {
                        int hi = Integer.parseInt(temp.split("AutoTestSoftware(?=\\d)")[1]);
                        hi += 8;
                        temp = "AutoTestSoftware" + hi;
                    }
                    world[k]=temp;
                    k++;
                }
                System.out.println(Arrays.toString(world)+"value of t: "+t);
                myEntries.set(t,world);
                t++;
            }
            System.out.println("\n\n");
            for (String[] element2 : myEntries)
            {
              System.out.println(Arrays.toString(element2));
            }