简体   繁体   English

如何将字符串值存储到数组字符串,并可以在另一个类中调用该数组以在单行中打印为字符串

[英]how to store string value to array string and can call that array in another class to Print as string in single line

I'm trying to do some work on Mobile apps.我正在尝试在移动应用上做一些工作。 I have parser in class: A我在class: A有解析器class: A

for (int i = 0; i < jArray3.length(); i++) {
    news_id = Integer.parseInt((json_data.getString("news_id")));
    news_title = json_data.getString("news_title");
}

I have to parse and get value of id and title.我必须解析并获取 id 和 title 的值。 Now I want to store this title in array and call another class.现在我想将此标题存储在数组中并调用另一个类。 In that class we have to convert that array into String so that i can print that title value in single line.在那个类中,我们必须将该数组转换为String以便我可以在一行中打印该标题值。

How can I implement this can u post some code for me?我该如何实现这一点,你可以为我发布一些代码吗?

Based on my understanding of your question, I assume the following snippet only you are looking for.根据我对您的问题的理解,我假设只有您正在寻找以下代码段。

String[] parsedData = new String[2];
for (int i = 0; i < jArray3.length(); i++) {
    news_id = Integer.parseInt((json_data.getString("news_id")));
news_title = json_data.getString("news_title");
}

parsedData[0] = news_id;
parsedData[1] = news_title;

DifferentCls diffCls = new DifferentCls(data);
System.out.println(diffCls.toString());

DifferentCls.java不同的Cls.java

private String[] data = null;

public DifferentCls(String[] data) {
 this.data = data;
}

public String toString() {
 return data[1];
}
 news_title = json_data.getString("news_title");

 Add line after the above line to add parse value  int0 string array

String[] newRow = new String[] {news_id ,news_title}; String[] newRow = new String[] {news_id ,news_title};

//Convert array to String //将数组转换为字符串

  String asString = Arrays.toString(newRow ) 

1. I am assuming that you have more than one title to be stored. 1.我假设您有多个标题要存储。

I am using ArrayList<String> which is more flexible than Array .我正在使用ArrayList<String> ,它Array更灵活

Creating an ArrayList and Storing all the title values:创建一个 ArrayList 并存储所有标题值:

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

for (int i = 0; i < jArray3.length(); i++) {
    news_id = Integer.parseInt((json_data.getString("news_id")));
    news_title = json_data.getString("news_title");

    titleArr.add(news_title);
}

2. Now sending it to another class , where you need to Show all the titles in Single Line. 2.现在将其发送到another class ,您需要在其中显示单行中的所有标题。

new AnotherClass().alistToString(titleArr);  


// This line should be after the for-loop
// alistToString() is a method in another class          

3. Another class structure. 3.另一种类结构。

 public class AnotherClass{

      //.................. Your code...........


       StringBuilder sb = new StringBuilder();
       String titleStr = new String();

    public void alistToString(ArrayList<String> arr){

    for (String s : arr){

     sb.append(s+"\n");   // Appending each value to StrinBuilder with a space.

          }

    titleStr = sb.toString();  // Now here you have the TITLE STRING....ENJOY !!

      }

    //.................. Your code.............

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM