简体   繁体   English

将多个值保存在数组中的相同索引处

[英]Holding multiple values at same index in array

My code retrieves a number of steps, then will output an instruction at each step via for loop. 我的代码检索多个步骤,然后将通过for循环在每个步骤输出一条指令。 There will be a title, description, video, and audio. 将有标题,说明,视频和音频。 However, I am trying to think of a way to store each variable into a structure. 但是,我正在尝试一种将每个变量存储到结构中的方法。 This is what I came up with, but I don't think an array can store multiple values at the same index. 这是我想出的,但我认为数组不能在同一索引中存储多个值。

for(int i = 0; i < QRData.number_steps; i++){
        System.out.println("Inside for loop");
        array[i].setTitle(btn_instructions.get(i));
        array[i].setDescription(instruction_row.get(i));
        array[i].setInstructionVideo(videos.get(i));
        array[i].setAudioInstruction(audio.get(i));
}

What would be a better way to store these values, so that they are easy to retrieve later? 有什么更好的方法来存储这些值,以便以后可以轻松检索它们? I'm pretty sure my method doesn't work. 我很确定我的方法行不通。

You should create a Object like: 您应该创建一个像这样的对象:

public class DataStorage{

  private String title;

  private String description;

  private File audioFile;

  private File videoFile;

  public DataStorage(String title, String description, File audioFile, File videoFile){
    this.title = title;
    this.description = description;
    this.audioFile = audioFile;
    this.videoFile = videoFile;
  }

  public String getTitle(){
    return this.title;
  }
 //similar for "description", "audioFile" and "videoFile"
 //...
}

For the DataStorage i would recommend using a ArrayList, because it is dynamic and you can add new elements while the Programm is running. 对于DataStorage,我建议使用ArrayList,因为它是动态的,您可以在Programm运行时添加新元素。 Then you can save Data in there with the loop: 然后,您可以使用循环将数据保存在其中:

ArrayList<DataStorage> storage = new ArrayList<>();

for(int i = 0; i < QRData.number_steps; i++){
    storage.add(new DataStorage(btn_instructions.get(i),instruction_row.get(i),
    videos.get(i),audio.get(i));
}

This is a basic concept of object oriented Programming to define classes to store custom data. 这是面向对象编程的基本概念,用于定义用于存储自定义数据的类。

You can now get the Data out of the Array by calling the "getters" of your Datastorage class. 现在,您可以通过调用Datastorage类的“获取器”将数据从数组中取出。 For example: 例如:

String title = storage.get(itemNumber).getTitle();

gives you the value of the "itemNumber" Title. 为您提供“ itemNumber”标题的值。

您应该创建一个新对象来存储这些值,然后将该对象存储在数组中。

You have to use objects arrays, using this you are going to create a class and an array of your class, for example check ne following code: 您必须使用对象数组,使用该对象,您将创建一个类和您的类的数组,例如,检查以下代码:

class Person{
private String name;
private Integer age;

Person(String name,Integer age){
     this.name=name;
     this.age=age;
}
----getters and setters methods-----

}

class Test{
public static void main(String args[]){
Person arr[]=new  Person[10];
arr[0]=new Person("Alex",26);
}
}

Check a complete example here http://www.java2s.com/Tutorial/Java/0200__Generics/ArraysStoringclassobjectsinArrayasdataitems.htm 在此处检查完整的示例http://www.java2s.com/Tutorial/Java/0200__Generics/ArraysStoringclassobjectsinArrayasdataitems.htm

Cheers 干杯

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

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