如何在Java中将String输出存储到数组[重复]

时间:2021-10-21 14:02:43

This question already has an answer here:

这个问题在这里已有答案:

I am getting a string output to this c.getString(0) method. I want to know how do I store this output into an array in Java? This c.getString(0) method is in a while loop. Each time the while loop runs, I want to store whatever the value I get with c.getString(0) method to store in an array. How can I do that?

我得到一个字符串输出到这个c.getString(0)方法。我想知道如何将此输出存储到Java中的数组中?这个c.getString(0)方法在while循环中。每次while循环运行时,我想用c.getString(0)方法存储我得到的任何值来存储在数组中。我怎样才能做到这一点?

private void showList() {

    DataBaseHelper db = new DataBaseHelper(this);
        db.open();
    c = db.retriveall();
    if (c.moveToFirst()) {
        do {
            Toast.makeText(MainActivity.this,
                    "_id: " + c.getString(0) + "\n",
                    Toast.LENGTH_LONG).show();
        } while (c.moveToNext());
    }

}

For now I'm showing it an toast message. Instead of showing I want to store it to an array. please help me

现在我正在向它展示一个祝酒词。而不是显示我想将它存储到数组。请帮我

4 个解决方案

#1


2  

To store the data you can do several things:

要存储数据,您可以执行以下操作:

  • Use a String Array, if you know the number of items.
  • 如果知道项目数,请使用字符串数组。

  • Use a String ArrayList, if you dont know the number of items.
  • 如果您不知道项目数,请使用String ArrayList。

If you are using the array, the declare the String array,

如果您正在使用该数组,则声明String数组,

String arr[]=new String[numberOfItems];

and then every time you are iterating the loop write:

然后每次你迭代循环写:

arr[position++]=c.getString(position);

Now if you are using Array List then Declare the Array List:

现在,如果您正在使用Array List,则声明Array List:

ArrayList<String> list=new ArrayList<String>();

and then every time you iterate through the loop:

然后每次遍历循环时:

list.add(c.getString(position));

After this to get the elements you can write a foreach loop.

在此之后获取元素,您可以编写一个foreach循环。

for(String ele: list){
   Toast.makeText(context,ele,Toast.LENGTH_SORT).show();
}

#2


1  

Use ArrayList. They are dynamic i.e. needs no initial length. Use the ArrayList.add() method to add elements to the ArrayList and use ArrayList.toArray() to get an array if you need one. More information from the official docs here.

使用ArrayList。它们是动态的,即不需要初始长度。使用ArrayList.add()方法向ArrayList添加元素,并使用ArrayList.toArray()获取数组(如果需要)。来自官方文档的更多信息。

#3


0  

Create one global variable like,

创建一个全局变量,如,

ArrayList<String> list = new ArrayList<String>();

Inside your loop, you can add this code,

在循环中,您可以添加此代码,

list.add(c.getString(0));

later you can convert it into array using list.toArray() method.

稍后您可以使用list.toArray()方法将其转换为数组。

If you want to store the String value by character wise then use toCharArray() method.

如果要按字符方式存储String值,请使用toCharArray()方法。

#4


0  

You can use below code:

您可以使用以下代码:

List<String> globalList = new ArrayList<String>();
private void showList() {

DataBaseHelper db = new DataBaseHelper(this);
    db.open();
c = db.retriveall();
if (c.moveToFirst()) {
    do {
        Toast.makeText(MainActivity.this,
                "_id: " + c.getString(0) + "\n",
                Toast.LENGTH_LONG).show();
        globalList.add(c.getString(0)) ;
    } while (c.moveToNext());
}

String [] a = globalList.toArray(new String[globalList.size()]);

}

#1


2  

To store the data you can do several things:

要存储数据,您可以执行以下操作:

  • Use a String Array, if you know the number of items.
  • 如果知道项目数,请使用字符串数组。

  • Use a String ArrayList, if you dont know the number of items.
  • 如果您不知道项目数,请使用String ArrayList。

If you are using the array, the declare the String array,

如果您正在使用该数组,则声明String数组,

String arr[]=new String[numberOfItems];

and then every time you are iterating the loop write:

然后每次你迭代循环写:

arr[position++]=c.getString(position);

Now if you are using Array List then Declare the Array List:

现在,如果您正在使用Array List,则声明Array List:

ArrayList<String> list=new ArrayList<String>();

and then every time you iterate through the loop:

然后每次遍历循环时:

list.add(c.getString(position));

After this to get the elements you can write a foreach loop.

在此之后获取元素,您可以编写一个foreach循环。

for(String ele: list){
   Toast.makeText(context,ele,Toast.LENGTH_SORT).show();
}

#2


1  

Use ArrayList. They are dynamic i.e. needs no initial length. Use the ArrayList.add() method to add elements to the ArrayList and use ArrayList.toArray() to get an array if you need one. More information from the official docs here.

使用ArrayList。它们是动态的,即不需要初始长度。使用ArrayList.add()方法向ArrayList添加元素,并使用ArrayList.toArray()获取数组(如果需要)。来自官方文档的更多信息。

#3


0  

Create one global variable like,

创建一个全局变量,如,

ArrayList<String> list = new ArrayList<String>();

Inside your loop, you can add this code,

在循环中,您可以添加此代码,

list.add(c.getString(0));

later you can convert it into array using list.toArray() method.

稍后您可以使用list.toArray()方法将其转换为数组。

If you want to store the String value by character wise then use toCharArray() method.

如果要按字符方式存储String值,请使用toCharArray()方法。

#4


0  

You can use below code:

您可以使用以下代码:

List<String> globalList = new ArrayList<String>();
private void showList() {

DataBaseHelper db = new DataBaseHelper(this);
    db.open();
c = db.retriveall();
if (c.moveToFirst()) {
    do {
        Toast.makeText(MainActivity.this,
                "_id: " + c.getString(0) + "\n",
                Toast.LENGTH_LONG).show();
        globalList.add(c.getString(0)) ;
    } while (c.moveToNext());
}

String [] a = globalList.toArray(new String[globalList.size()]);

}