无法对数组的更新方式进行排序



嗨,我有一些人的工作代码,它可以正常工作,我认为这个特定的代码正在更新temp[]的值。但我无法精确地计算temp[i][j]=更新后的结果。因为该方法没有返回任何东西,也没有赋值给temp[][]。我看到的只是update_table[][]=temp;

当我尝试在循环之前和循环之后打印temp时,数组会发生变化。

void var_init(String to_match_x,String to_match_y, String to_replace_x, String to_replace_y,String[][] temp)
{
    String t_match_x=to_match_x;
    String t_replace_x=to_replace_x;
    String t_match_y=to_match_y;
    String t_replace_y=to_replace_y;
    //String str=string;
    //add function to count variables find duplicates and assign values to them
    line();
    System.out.println("text to match:"+t_match_x);
    System.out.println("text to replace with:"+t_replace_x);
    System.out.println("text to match:"+t_match_y);
    System.out.println("text to replace with:"+t_replace_y);
    String[][] table_update=temp;
    line();
    System.out.println("starting fetching rules for updating variable");
    line();
    for(int i=0;i<table_update.length;i++)
        for(int j=0;j<table_update[0].length;j++)
            {
                 String replace_text=table_update[i][j];
                 System.out.println(replace_text);
                 String new_str;
                 new_str=replace_text.replaceAll("\"+t_match_y,t_replace_y);         
                 new_str=new_str.replaceAll("\"+t_match_x,t_replace_x);
                 table_update[i][j]=new_str;
                 System.out.println(table_update[i][j]);
                 line();
            }
}

您正在引用一个名为temp 的数组

String[][] table_update=temp;

一旦你更新了table_update数组,temp也会被更新。如果你不想发生这种情况,请像这个一样克隆temp数组

String[][] table_update = temp.clone();
  String[][] table_update=temp;

这意味着table_update和temp都有相同的引用,所以如果其中一个更新了,那么它们实际上都是同一个数组,而另一个也应该更新。您可以创建一个新的数组(temp)并将table_update克隆到其中,以解决您的问题。

相关内容

最新更新