【问题标题】:How to add data to a custom ArrayList using three lists?如何使用三个列表将数据添加到自定义 ArrayList?
【发布时间】:2021-10-27 18:20:06
【问题描述】:

我有三个 String Lists ,我想用它们来填充 ArrayList 的 ActorData (POJO 类)类型,如何正确完成此操作?

数据示例

演员姓名

actornames = {
0 = "name1",
1 = "name2"
}

演员角色

actorcharacters = {
0 = "character1",
1 = "character2"
}

演员照片文件路径

actorphotoFILEpaths = {
0 = "filepath1",
1 = "filepath2"
}

我的问题是在activity class 的onCreate 中使用以下方法收集所有三个列表,即生成的actors 列表是这样的:

actors = [
0 = {
     name = "name1",
     character = null,
     photofilepath = null
    },
1 = {
     name = "name2",
     character = null,
     photofilepath = null
    },
2 = {
     name = null,
     character = "character1",
     photofilepath = null
    },
3 = {
     name = null,
     character = "character2",
     photofilepath = null
    },
4 = {
     name = null,
     character = null,
     photofilepath = "photofilepath1"
    },
5 = {
     name = null,
     character = null,
     photofilepath = "photofilepath2"
    }
]

但我希望输出是这样的

actors = [
0 = {name = "name1",
     character = "character1",
     photofilepath = "photofilepath1"
    },
1 = {name = "name2",
     character = "character2",
     photofilepath = "photofilepath2"
    }
]

活动

public void onCreate(Bundle savedInstanceState) {

    rest of the code ......

    List<String> actornames = new ArrayList<>();
    List<String> actorcharacters = new ArrayList<>();
    List<String> actorphotoFILEpaths = new ArrayList<>();
    List<ActorData> actors = new ArrayList<ActorData>();        

    // Initializing actornames arraylist
    String actorname = mMovie.getActorname();
    actornames = Arrays.asList(actorname.split("\\s*,\\s*"));

    // Initializing actorcharacters arraylist
    String actorcharacter = mMovie.getActorcharacter();
    actorcharacters = Arrays.asList(actorcharacter.split("\\s*,\\s*"));

    // Initializing actorphotoFILEpaths arraylist
    String baseFILEpath = "/storage/emulated/0/Android/data/com.miz.mizuu/files/movie-actors/";
    String actorphotourl = mMovie.getActorphotourl();
    actorphotoFILEpaths = Arrays.asList(actorphotourl.split("\\s*,\\s*"));        

    List<ActorData> actors = new ArrayList<ActorData>();
    for (int i = 0; i < actornames.size(); i++) {
    ActorData actorData = new ActorData();
    String name = actornames.get(i);
    actorData.setName(name);
    actors.add(actorData);
    }
    for (int i = 0; i < actorcharacters.size(); i++) {
    ActorData actorData = new ActorData();
    String character = actorcharacters.get(i);
    actorData.setCharacter(character);
    actors.add(actorData);
    }
    for (int i = 0; i < actorphotoFILEpaths.size(); i++) {
    ActorData actorData = new ActorData();
    String filepath = baseFILEpath + actorphotoFILEpaths.get(i).replaceAll("/", "");
    actorData.setPhotoFILEpath(filepath);
    actors.add(actorData);
    } 
}        

POJO 类

public class ActorData {
    String name, character, photofilepath;
    public ActorData() {}

    public String getName() {return name;}

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

    public String getCharacter() {return character;}

    public void setCharacter(String character) {this.character = character;}

    public String getPhotoFILEpath() {return photofilepath;}

    public void setPhotoFILEpath(String photofilepath) {this.photofilepath = photofilepath;}
}

【问题讨论】:

  • 合并for-cycles(?)
  • 谢谢,但我不知道怎么做,你能帮忙

标签: java android list arraylist


【解决方案1】:

tl;博士

record Actor( String name , String character , Path filePath ) { }

List < String > names = List.of( "Alice" , "Bob" );
List < String > characters = List.of( "Juliet" , "Romeo" );
List < Path > paths = List.of( Paths.get( "filepath1" ) , Paths.get( "filepath2" ) );

if ( ( names.size() + characters.size() + paths.size() ) != ( names.size() * 3 ) )
{
    throw new IllegalStateException( "Input list sizes differ. Message # 075e7165-09d5-4256-8e5e-b5431520b1b4." );
}

List < Actor > actors = new ArrayList <>( names.size() );
for ( int index = 0 ; index < names.size() ; index++ )
{
    actors.add(
            new Actor( names.get( index ) , characters.get( index ) , paths.get( index ) )
    );
}

actors = List.copyOf( actors );

记录

Java 16 及更高版本提供了一种更简洁的方法来编写一个主要目的是透明且不可变地传递数据的类。您只需声明每个成员字段的名称和类型。编译器隐式创建默认构造函数、getter、equals & hashCode 和 toString。见JEP 395: Records。

整个记录定义:

record Actor( String name , String character , Path filePath ) { }

对于操作路径和文件,了解如何使用 Java NIO.2 类而不是遗留类。 Path 可能是满足您需求的合适课程。见tutorial by Oracle。

List.of

制作一些样本数据,使用List.of写成unmodifiable lists。

List < String > names = List.of( "Alice" , "Bob" );
List < String > characters = List.of( "Juliet" , "Romeo" );
List < Path > paths = List.of( Paths.get( "filepath1" ) , Paths.get( "filepath2" ) );

验证输入

验证所有输入列表的大小是否相同。

if ( ( names.size() + characters.size() + paths.size() ) != ( names.size() * 3 ) )
{
    throw new IllegalStateException( "Input list sizes differ. Message # 075e7165-09d5-4256-8e5e-b5431520b1b4." );
}

实例化对象并收集

还有核心逻辑,将三个输入列表中的每一个中的元素组装成一个新的Actor 对象。

List < Actor > actors = new ArrayList <>( names.size() );
for ( int index = 0 ; index < names.size() ; index++ )
{
    actors.add(
            new Actor( names.get( index ) , characters.get( index ) , paths.get( index ) )
    );
}

转储到控制台。

System.out.println( "actors = " + actors );

运行时。

演员 = [演员[name=Alice, character=Juliet, filePath=filepath1], Actor[name=Bob, character=Romeo, filePath=filepath2]]

通常最好返回一个不可修改的列表。使用List.copyOf。

return List.copyOf( actors ) ;

【讨论】:

  • 非常感谢先生,我累了List &lt; String &gt; names = List.of( "Alice" , "Bob" ); 但of 无法解决,请问先生,我使用 java 1.7 可能是什么问题?
  • @Steve 使用latest version of Javadoc 可以看到List.of 已添加到Java 9。List.copyOf 已添加到Java 10。您会在第三方库的类中找到类似的方法,例如谷歌番石榴或 Eclipse 集合。我不知道 Java 7 是否仍在维护这些内容。我建议您考虑迁移到 Java 的现代 LTS 版本:8、11 和 17。
  • 现在我被 7 困住了,所以我应该改用 Arrays.asList,先生,看来我应该专门为 Path 创建一个 pojo 类,对吧?
【解决方案2】:

我通过结合for loops 完成了它

     List<ActorData> actors = new ArrayList<>();
                for (int i = 0; (i < actornames.size()) && (i < actorcharacters.size()) && (i < actorphotoFILEpaths.size()); i++) {
                    ActorData actorData = new ActorData();
                    String name = actornames.get(i);
                    String character = actorcharacters.get(i);
                    String filepath = baseFILEpath + actorphotoFILEpaths.get(i).replaceAll("/", "");
                    actorData.setName(name);
                    actorData.setCharacter(character);
                    actorData.setPhotoFILEpath(filepath);
                    actors.add(actorData);
                }

输出

actors = [
0 = {
     name = "name1",
     character = "character1",
     photofilepath = "photofilepath1"
    },
1 = {
     name = "name2",
     character = "character2",
     photofilepath = "photofilepath2"
    }
]

【讨论】:

  • 在for 循环之前,检查所有列表的大小是否相同。然后您的for 循环限制可以设置为列表大小的任何一个。
  • @BasilBourque 感谢您的意见,先生,您的意思是这样if (actornames.size() == actorcharacters.size() &amp;&amp; actorcharacters.size() == actorphotoFILEpaths.size()) { List&lt;ActorData&gt; actors = new ArrayList&lt;&gt;(); ..... }
  • 如果不相等,则不会将数据添加到actors,这样比较好。谢谢
猜你喜欢
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 2011-02-17
  • 1970-01-01
相关资源
最近更新 更多