标签: arraylist

将 JSON 数据添加到 arrayList

仍在处理我脚本的这一部分以解析我的 JSON jsonArray = new JSONArray(jsonData);,然后将其添加到数组列表“myJSONArray”中

我不确定 for 循环如何将我的元素从 jsonArray 添加到 myJSONArray (arrayList)

如果我不清楚,请告诉我,如果我遗漏了任何信息,请询问。提前致谢。

JSON 数据:

{"id":["1","2","3"],"name":["Dragon","Butterfly","Tattoo"],"thumb":["thm_polaroid.jpg","thm_default.jpg","thm_enhanced-buzz-9667-1270841394-4.jpg"],"path":["polaroid.jpg","default.jpg","enhanced-buzz-9667-1270841394-4.jpg"]}
Run Code Online (Sandbox Code Playgroud)

image_data 类:

public class image_data {


    public int id;
    public String name;
    public String thumb;
    public String path;


}
Run Code Online (Sandbox Code Playgroud)

ShowThumb 类:

public class showThumb extends Activity{



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gridlayout);

         Bundle bundle = getIntent().getExtras();
         String jsonData = bundle.getString("jsonData");


         JSONArray jsonArray = null;
        try {
            jsonArray = new JSONArray(jsonData);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace(); …
Run Code Online (Sandbox Code Playgroud)

java android json arraylist

1
推荐指数
1
解决办法
2万
查看次数

使用 ArrayLists 的 @ResponseBody 序列化错误

尝试使用 SpringMVC @ResponseBody 将我的对象作为 JSON 字符串返回时出现以下错误:

org.codehaus.jackson.map.JsonMappingException:没有找到类 com.ResourceResultSetCol 的序列化程序,也没有发现创建 BeanSerializer 的属性(为了避免异常,禁用 SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS))(通过参考链:com.medplus.devops.pdt .server.ResourceResultSet["cols"]->java.util.ArrayList[0])

图结果集.java:

@Controller
@RequestMapping("/pdt")
public class GraphResultSet {

    @RequestMapping(value = "/getResourceResultSet", method = RequestMethod.GET)
    public @ResponseBody
    ResourceResultSet getResourceResultSet(
            @RequestParam(value = "resourceId", required = true) int resourceId) {
        return new ResourceResultSet(resourceId);
    }

}
Run Code Online (Sandbox Code Playgroud)

资源结果集.java:

public class ResourceResultSet implements Serializable {

    public String resourceName;
    public ArrayList<ResourceResultSetCol> cols;
    public ArrayList<ResourceResultSetRow> rows;

    ResourceResultSet(int id) {
        resourceName = "Graph " + id;
        cols = new ArrayList<ResourceResultSetCol>();
        cols.add(new ResourceResultSetCol("col1","Timestamp","date"));
        cols.add(new ResourceResultSetCol("col2","Value","number"));

        int randomNumberOfResults = …
Run Code Online (Sandbox Code Playgroud)

java serialization arraylist spring-mvc jackson

1
推荐指数
1
解决办法
5594
查看次数

如何从最小到最大数字对数字进行排序 - Java?

我如何将数字保存在列表中,我可以在其中从最小到最大排序。我创建了新字符串,我在其中插入了所有数字。比我进入 List 并对其进行排序。之后我检查一个数字,我得到颜色 od 图标。

我试过这个:

import java.util.Collections;
import java.util.ArrayList;
import java.io.*;

int colour1 = 1;
int colour2 = 3;
int colour3 = 2;
int colour4 = 3;

String rezerva =colour1, colour2, colour3, colour4;
List<String> myList = new ArrayList(rezerva);
Collections.sort(myList);

colour1 = myList.get(0);

if (colour1==1){
//change icon to red
}
else if (colour1==2){
//change icon to white
}
else {
//change icon to black
}
Run Code Online (Sandbox Code Playgroud)

java arrays sorting collections arraylist

1
推荐指数
1
解决办法
2659
查看次数

避免列表中的重复项

我试图避免列表中的重复值,但它不起作用。你能帮我解决这个问题吗?我做了很多工作,但我是 android 的新手,也是编码的新手。你能帮我在我犯错的地方吗?

import java.util.ArrayList;

public class Dynamic extends Activity {

    Button bt, bt2, ent, clr;
    EditText et, et2;
    String input, input1;
    public static ArrayList<String> k_listItems = new ArrayList<String>();
    public static ArrayList<String> p_listItems = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bt = (Button) findViewById(R.id.nameok);
        et = (EditText) findViewById(R.id.name);

        input = et.getText().toString();

        bt.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                if (et.getText().length() == 0) {
                    Context context = getApplicationContext();
                    CharSequence text = "Enter Your Name";
                    int duration …
Run Code Online (Sandbox Code Playgroud)

android arraylist duplicates

1
推荐指数
1
解决办法
3782
查看次数

多线程编辑 ArrayList 及其对象

我正在尝试运行 2 个并发线程,其中一个不断向列表添加对象,另一个更新这些对象,并可能从列表中删除其中一些对象。我有一个ArrayList用于我的方法和类的整个项目,所以现在很难改变它。

我环顾四周,发现了几种方法可以做到这一点,但正如我所说,很难从ArrayList. 我尝试使用synchronized和notify()将对象添加到列表wait()中的方法,以及更改这些对象的方法,如果它们满足某些条件,则可能会删除它们。

现在,我已经想出了如何使用 a 来做到这一点CopyOnWriteArrayList,但我想知道是否有可能使用ArrayList它自己来模拟这一点。这样我就不必编辑我的整个代码。

所以,基本上,我想做这样的事情,但是ArrayList:

import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;

public class ListExample{
    CopyOnWriteArrayList<MyObject> syncList;

    public ListExample(){
        syncList = new CopyOnWriteArrayList<MyObject>();

        Thread thread1 = new Thread(){
            public void run(){
                synchronized (syncList){
                    for(int i = 0; i < 10; i++){
                        syncList.add(new MyObject(i));
                    }
                }
            }
        };

        Thread thread2 = new Thread(){
            public void run(){
                synchronized (syncList){
                    Iterator<MyObject> iterator = syncList.iterator();
                    while(iterator.hasNext()){
                        MyObject …
Run Code Online (Sandbox Code Playgroud)

java multithreading arraylist synchronized wait

1
推荐指数
1
解决办法
6012
查看次数

Java Arraylist/List 错误

我已经标记了给我带来麻烦的那条线

private void EditButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
Run Code Online (Sandbox Code Playgroud)

DefaultListModel PatientListModel = new DefaultListModel();

for (Patient s : PatientList) {
    int AccNum = Integer.parseInt(IDTextField.getText());

    if (AccNum == s.getAccountNumber()) {

        s.setName(NameTextField.getText());
        s.setAge(Integer.parseInt(AgeTextField.getText()));
        s.setAddress(AddressTextField.getText());
        String PatientSex = "";

        if (MaleRadioButton.isSelected()) {
            PatientSex = "Male";
        }

        if (FemaleRadioButton.isSelected()) {
            PatientSex = "Female";
        }

        s.setSex(PatientSex);
        s.setPhone(PhoneTextField.getText());
        ArrayList<PatientCondition> PatientConditions3 = new ArrayList();
  ===>      PatientConditions3 = (ArrayList<PatientCondition>) ConditionsJList.getSelectedValuesList(); //error here
        s.setConditionsList(PatientConditions3);
        PatientInfoLabel2.setText("Patient Details Updated");

        for (Patient f : PatientList) {
            PatientListModel.addElement(f.getAccountNumber() + "-" + f.getName());
        }

        PatientJList.setModel(PatientListModel);
        UpdateAllViews();

        // …
Run Code Online (Sandbox Code Playgroud)

java netbeans exception arraylist

1
推荐指数
1
解决办法
6036
查看次数

无法添加到数组列表

我有一个最奇怪的问题,可能有一个简单的解决方案。

我创建并初始化了一个列表,然后继续创建列表类型的 4 个对象。在这些的构造函数中,它们将自己放在列表中。或者至少应该这样做。我总是得到一个越界异常,我不知道为什么。我将列表的大小设置为 402(对于所有可能的 VK 值),但在控制台和调试中,它总是说它的大小为 0,无论我设置它有多大或多空......

public class InputHandler implements KeyListener
{

public static List<Key> keyList = new ArrayList<Key>(KeyEvent.KEY_LAST);

public Key up = new Key(KeyEvent.VK_UP);
public Key down = new Key(KeyEvent.VK_DOWN);
public Key left = new Key(KeyEvent.VK_LEFT);
public Key right = new Key(KeyEvent.VK_RIGHT);


public class Key
    {

    public int keyCode;

    public Key(int defaultCode)
    {
        this.keyCode = defaultCode;
        keyList.add(keyCode,this);
    }

    public Key reMapKey(int newKey)
    {
        keyList.remove(keyCode);
        keyList.set(newKey, this);
        this.keyCode = newKey;
        return this;
    }

}

}
Run Code Online (Sandbox Code Playgroud)

代码还有更多内容,但我尝试 SSCCE 它。 …

java list arraylist indexoutofboundsexception

1
推荐指数
1
解决办法
4638
查看次数

为什么这段代码不起作用?

大家好我写了这个代码,我有两个错误.

  1. 无效的排名说明符:预期','或']'
  2. 无法将带有[]的索引应用于类型为"int"的表达式

你能帮帮忙吗?

    static void Main(string[] args)
    {
        ArrayList numbers = new ArrayList();

        foreach (int number in new int[12] {10,9,8,7,6,5,4,3,2,1}) //error No.1
        {
            numbers.Add(number);
        }

        numbers.Insert(numbers.Count - 1, 75);
        numbers.Remove(7);
        numbers.RemoveAt(6);

        for(int i=0; i<numbers.Count; i++)
        {
            int number = (int) number[i]; // error No.2
            Console.WriteLine(number);
        }
    }
Run Code Online (Sandbox Code Playgroud)

.net c# arrays arraylist

0
推荐指数
1
解决办法
529
查看次数

从ArrayList或LinkedList中删除元素是否更有效?

从理论上讲,从a ArrayList或a中删除元素是否更有效LinkedList?

java linked-list arraylist

0
推荐指数
2
解决办法
486
查看次数

最有效的方法来反转堆栈并添加到ArrayList

我有两个集合 - 一个ArrayList和一个Stack.我使用堆栈因为我需要一些简单的pop/push功能来代替这段代码.ArrayList本质上是out变量,因为这是函数中的一小部分代码.

因此,变量是这样定义的,然后运行代码以向堆栈添加元素.

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

/* other code.. */

Stack<String> lineStack = new Stack<String>();

/* code that adds stuff to the stack */
Run Code Online (Sandbox Code Playgroud)

问题是,现在我有一个完全填充的堆栈,如何以相反的顺序将它放在out ArrayList中,然后从pop顺序放置.

我的第一个想法是解决方案

 while(!lineStack.empty()) {
     out.add(0, lineStack.pop());
 }
Run Code Online (Sandbox Code Playgroud)

...哪个有效,但我担心在ArrayList的开头添加一个元素的效率(这会强制所有现有元素需要转移..这是一个链表(我相信)..大不了...但仍然在意).此外,我正在通过循环运行...可能不必要.

所以,我的第二个解决方案不涉及循环(至少在我的代码中,我确定后端调用是这样做的).

 List l = lineStack.subList(0, lineStack.size());
 out.addAll(l);
Run Code Online (Sandbox Code Playgroud)

我知道我不需要分配列表,但它会保留更清晰的代码.但是,我不确定这是否会给我带来特别有用的性能提升.

所以,我的问题是:对于SMALL到MEDIUM尺寸的设置中哪一个最有效?如果有更有效的解决方案,它会是什么?

java performance stack arraylist data-structures

0
推荐指数
1
解决办法
2万
查看次数