【问题标题】:How to make array of nested struct in java?如何在java中制作嵌套结构数组?
【发布时间】:2020-03-11 08:36:24
【问题描述】:

如何在java中制作这样的数组结构?以及如何在 Main Class 中启动它?

struct Channel{

    char email[100];
    char password[100];
    char channelName[100];
    char subscriber[][100];
    int numberOfSubscriber;
    int numberOfVideos;

    struct Video{
         String videoId ;
         char videoName ;
         char videoDuration ;
         char videoTag ;
    }video[100];

}channel[100];
// i need to make it like this ( channel[i].video[j].*** )

【问题讨论】:

  • java中没有结构体
  • 我的意思是如何使用类?

标签: java arrays class struct nested


【解决方案1】:

您在 java 中创建类。

您可以在此处使用java.util.List 而不是使用数组,它允许您动态添加或删除元素。

这是一个例子。您可以通过添加构造函数、将字段设为私有以及添加公共 getter 和 setter 来控制数据流来对此进行改进。

import java.util.ArrayList;
import java.util.List;

class Video {
    String videoId;
    String videoName;
    String videoDuration;
    String videoTag;
}

class Channel {
    String email;
    String password;
    String channelName;
    List<String> subscriber = new ArrayList<>();
    List<Video> videos = new ArrayList<>();

    public int numberOfSubscriber() {
        return subscriber.size();
    }
    public int numberOfVideos() {
        return videos.size();
    }
}

public class Main {
    public static void main(String[] args) {
        // create a new channel
        Channel channel = new Channel();

        // modify some variables
        channel.email = "example@example.com";
        channel.subscriber.add("subscriber 1");

        // create a new video
        Video video = new Video();
        video.videoName = "this is a video";

        // add video to channel
        channel.videos.add(video);

        // get number of videos
        System.out.println(channel.numberOfVideos());
    }
}

【讨论】:

    【解决方案2】:

    Java 中没有结构。你将不得不使用类。试试这个:

    public class Channel {
        private String email;
        private String password;
        public Channel (String email, String password) {
            this.email = email;
            this.password = password;
        }
    }
    

    然后在main中:

    Channel[] channels = new Channel[100];
    for(int i = 0; i < channels.length; i++)
        channels[i] = new Channel(...);
    

    Video也是一个类,Channel类中会有一个Video[]类型的字段

    【讨论】:

      【解决方案3】:
      There is no struct in Java. Rather, you have to use class to do it.
          public class Channel {
              public char email[] = new char[100];
              public char password []= new char[100];
              public char channelName[]= new char[100];
              public char subscriber[][] = new char[100][];
              public int numberOfSubscriber;
              public int numberOfVideos;
      
              public Video[] videos = new Video[100];
      
              public static class Video {
                  public String videoId ;
                  public String videoName ;
                  public String videoDuration ;
                  public String videoTag ;
              }
      
              public static void main(String[] args) {
      
                  //For Initilizing
                  Channel[] channels = new Channel[100];
                  for(int i = 0; i< 100; i++) {
                      channels[i] = new Channel();
                      //set values of channel
                      for(int j=0; j<100;j++) {
                          channels[i].videos[j] = new Video();
                          //set values of videos
                      }
                  }
      
                  // you can retrieve the information in the same way
              }
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-14
        • 2022-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-14
        • 1970-01-01
        相关资源
        最近更新 更多