Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何向特定数组添加新字符串_Java_Android_Arrays_String_Add - Fatal编程技术网

Java 如何向特定数组添加新字符串

Java 如何向特定数组添加新字符串,java,android,arrays,string,add,Java,Android,Arrays,String,Add,我的代码中有4个数组,每当用户在edittext中写入内容时,我想将该字符串存储在其中一个数组中,我尝试使用ToCharray方法,但我不知道如何定义该字符串应放在的数组:S String [] array7 = {"Hey","Was Up","Yeahh"}; TextView txtV1,txtV2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCr

我的代码中有4个数组,每当用户在edittext中写入内容时,我想将该字符串存储在其中一个数组中,我尝试使用ToCharray方法,但我不知道如何定义该字符串应放在的数组: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

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

ArrayList<String> stringList = new ArrayList<String>();
stringList.add("Text here");
ArrayList stringList=新建ArrayList();
stringList.添加(“此处文本”);

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

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

array7[0] = value; //or:
array7[1] = value; //or:
array7[1] = 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;
//创建一个比原始数组大的新数组。
String[]newArray7=新字符串[array7.length+1/*1是您需要的最小值,但最好添加更多。两倍于当前长度是个好主意*/];
//将旧数组的内容复制到新数组中。
for(int i=0;i

您可以在单独的方法中执行此操作,因此无论何时需要重新调整阵列的大小,都可以使用它。您应该知道,类ArrayList和Vector已经为您实现了这种机制,您可以
ArrayList.add(string)
任意添加。

一些代码可以确保这个问题有意义。谢谢:)这非常有帮助!!他是认真的。您的问题不清楚,请将代码复制并粘贴到问题中,或者至少尝试更具体一些-数组是什么类型,如何决定每个字符串指向哪个数组等。您不知道是谁否决了您的问题。可能是其他人。它是这个网站的一部分,每个人都会遇到。甚至我……:)