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

在特定索引处的ArrayList中添加和设置新对象(JAVA),java,object,arraylist,set,add,Java,Object,Arraylist,Set,Add,我用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,亚特兰大,佐治亚州。位置将在索引1处。然后其他对象将在其他

我用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,亚特兰大,佐治亚州。位置将在索引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);
        }
    }

}
publicstaticvoidmain(字符串[]args){
ArrayList newsTVList=新的ArrayList();
字符串nameToAdd=“CNN”;
字符串idToAdd=“1234-123X”;
字符串cityToAdd=“亚特兰大”;
字符串stateToAdd=“GA”;
int indexToAdd=6;
……这一部分,我添加了对象,所以不用担心它们。
newsTVList.add(indexToAdd,null);
插入对象(newsTVList、indexToAdd、nameToAdd、idToAdd、cityToAdd、stateToAdd);
公共静态void insertObject(ArrayList np、int索引、字符串n、字符串id、,
字符串c、字符串s){
对于(新闻电视新闻:np){
if(np.indexOf(索引)){
新闻集名(n);
新闻,setISSN(id),;
新闻.塞西蒂(c);
新闻.设置状态(s);
}
}
}

首先创建POJO类,这里是公司

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;
    }
}
现在将company添加到Arraylist中,如下所示

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);
    }
}
package com.appkart.array;
导入java.util.ArrayList;
公共类测试公司{
ArrayList公司=新的ArrayList();
上市公司(){
公司纽约时报=新公司(“纽约时报”,“1234”,
“纽约”、“纽约”);
nbc公司=新公司(“nbc费城”、“X123”、“费城”,
“PA”);
福克斯公司=新公司(“福克斯新闻”、“0987”、“洛杉矶”、“洛杉矶”);
公司。添加(纽约时报);
美国国家广播公司;
添加(福克斯);
printCompanyInfo();
}
公开作废AddCompanyIndex(内部索引,公司){
公司。添加(索引,公司);
printCompanyInfo();
}
public void printCompanyInfo(){
适用于(公司:公司){
System.out.println(company.toString());
}
}
公共静态void main(字符串[]args){
TestCompany TestCompany=新的TestCompany();
testCompany.addCompany();
公司=新公司(“CNN”、“1230”、“亚特兰大”、“GA”);
testCompany.AddCompanyIndex(1,公司);
}
}

当您使用
newsTVList.add(indexToAdd,null);
您正在向arraylist添加一个null项,并且当您使用
for(NewsTV news:np){if(np.indexOf(index))
其中一个np将为null,程序将抛出null指针异常

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

String nameToAdd = "CNN"; 
String idToAdd = "1234-123X"; 
String cityToAdd = "Atlanta"; 
String stateToAdd = "GA";
然后调用
newsTVList.add(indexToAdd,);

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


哦,你不必添加班级公司,但我觉得没问题。无论如何,我不知道主课的某些部分。它适用于每个项目。谢谢,伙计!