在 ArrayList 中的特定索引处添加和设置新对象 (JAVA)

Adding and setting new object in ArrayList at specific index (JAVA)

我用 ArrayList 做了一些项目。他们在添加新对象时遇到同样的问题。我的目标是在特定位置添加新对象。例如,每个索引包含四个字符串。

index 0: New York Times, 1234, New York, NY
index 1: NBCPhiladelphia, X123, Philadelphia, PA
index 2: FOX News, 0987, Los Angeles, LA

假设我想添加一个新的:CNN,1230,Atlanta,GA。该位置将位于索引 1。然后其他对象将移动到其他索引,依此类推...像这样:

index 0: New York Times, 1234, New York, NY
index 1: CNN, 1230, Atlanta, GA
index 2: NBCPhiladelphia, X123, Philadelphia, PA
index 3: FOX News, 0987, Los Angeles, LA

到目前为止,我的代码似乎无法插入新代码。我不知道如何找到解决此错误的方法。

public static void main(String[] args) {
ArrayList<NewsTV> newsTVList = new ArrayList<NewsTV>();
String nameToAdd = "CNN";
    String idToAdd = "1234-123X";
    String cityToAdd = "Atlanta";
    String stateToAdd = "GA";
    int indexToAdd = 6;

...... //This part, I add objects so don't worry about them.

    newsTVList.add(indexToAdd, null);
    insertObject(newsTVList, indexToAdd, nameToAdd, idToAdd, cityToAdd, stateToAdd);

public static void insertObject(ArrayList<NewsTV> np, int index, String n, String id,
                                    String c, String s) {

    for(NewsTV news: np) {
        if(np.indexOf(index)) {
            news.setName(n);
            news.setISSN(id);
            news.setCity(c);
            news.setState(s);
        }
    }

}

首先创建POJOClass,这里是Company

package com.appkart.array;

public class Company {

    private String name;
    private String id;
    private String city;
    private String state;

    public Company(String name, String id, String city, String state) {
        this.name = name;
        this.id = id;
        this.city = city;
        this.state = state;

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Override
    public String toString() {
        return name + ", " + id + ", " + city + ", " + state;
    }
}

然后现在将公司添加到Arraylist as

package com.appkart.array;

import java.util.ArrayList;

public class TestCompany {
    ArrayList<Company> companies = new ArrayList<Company>();

    public void addCompany() {
        Company newYorkTimes = new Company("New York Times", "1234",
                "New York", "NY");
        Company nbc = new Company("NBCPhiladelphia", "X123", "Philadelphia",
                "PA");
        Company fox = new Company("FOX News", "0987", "Los Angeles", "LA");

        companies.add(newYorkTimes);
        companies.add(nbc);
        companies.add(fox);

        printCompanyInfo();
    }

    public void addCompanyAtIndex(int index, Company company) {
        companies.add(index, company);

        printCompanyInfo();
    }

    public void printCompanyInfo() {
        for (Company company : companies) {
            System.out.println(company.toString());
        }
    }

    public static void main(String[] args) {
        TestCompany testCompany = new TestCompany();
        testCompany.addCompany();

        Company company = new Company("CNN", "1230", "Atlanta", "GA");
        testCompany.addCompanyAtIndex(1, company);
    }
}

当您使用 newsTVList.add(indexToAdd, null); 时,您将向数组列表中添加一个空项,而当您使用 for(NewsTV news: np) {if(np.indexOf(index)) 时,其中一个 np 将为空,程序将抛出空指针异常。

创建一个新的 NewsTV 对象并用它的值初始化它

String nameToAdd = "CNN"; 
String idToAdd = "1234-123X"; 
String cityToAdd = "Atlanta"; 
String stateToAdd = "GA";

然后调用newsTVList.add(indexToAdd, <Your new NewsTV object> );

如果您需要 ArrayList.add(int index, E element) 方法的详细信息,请查看 JSE 7 的文档

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html