-3

我的代码中有 4 个数组,每次用户在 edittext 中写入一些内容时,我想将该字符串存储在其中一个数组中,我尝试使用 toCharArray 方法,但我不知道如何定义字符串应该放置的数组放入:S

String [] array7 = {"Hey","Was Up","Yeahh"};
    TextView txtV1,txtV2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layouttry);
        txtV1=(TextView)findViewById(R.id.textView1);
        txtV2=(TextView)findViewById(R.id.textView2);



        Bundle extras = getIntent().getExtras();
        String value = extras.getString("Key");  // this value I want to add to the stringarray
4

2 回答 2

2

如果您需要添加新元素,我建议您使用 ArrayLists 替换您的数组。这将允许您使用该add方法插入新元素。一个例子:

ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Text here");
于 2013-04-07T18:31:17.367 回答
0

在您的代码中,我只能在字符串上看到一个数组,所以我不确定您实际需要什么。不过我会尽力的。

您的 String 数组被硬编码为只有三个单元格,并且它们都已满。如果要将字符串放入这些位置中的任何一个,请执行以下操作:

array7[0] = value; //or:
array7[1] = value; //or:
array7[1] = value;

如果要添加value到数组而不删除现有值,可以执行以下操作:

//Create a new array, larger than the original.
String[] newArray7 = new String[array7.length + 1 /*1 is the minimum you are going to need, but it is better to add more. Two times the current length would be a good idea*/];

//Copy the contents of the old array into the new one.
for (int i = 0; i < array7.length; i++){
   newArray7[i] = array7[i];
}

//Set the old array's name to point to the new array object.
array7 = newArray7;

您可以在单独的方法中执行此操作,因此每当您需要重新调整数组大小时,您都可以使用它。你应该知道 ArrayList 和 Vector 类已经为你实现了这个机制,你可以arrayList.add(string)随心所欲。

于 2013-04-07T19:50:55.503 回答