如何在java中制作嵌套结构的数组?

如何在java中制作嵌套结构的数组?,java,arrays,class,struct,nested,Java,Arrays,Class,Struct,Nested,如何在java中创建这样的数组结构?在主课堂上如何启动它 struct Channel{ char email[100]; char password[100]; char channelName[100]; char subscriber[][100]; int numberOfSubscriber; int numberOfVideos; struct Video{ String videoId ;

如何在java中创建这样的数组结构?在主课堂上如何启动它

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中没有结构。您必须使用类。试试这个:

public class Channel {
    private String email;
    private String password;
    public Channel (String email, String password) {
        this.email = email;
        this.password = password;
    }
}
然后大体上:

Channel[] channels = new Channel[100];
for(int i = 0; i < channels.length; i++)
    channels[i] = new Channel(...);
Channel[]channels=新频道[100];
对于(int i=0;i
视频也将是一个类,在
频道中会有一个
Video[]
类型的字段
class

Java中没有结构。相反,您必须使用类来完成它。
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
        }
    }
公共类频道{ 公共字符电子邮件[]=新字符[100]; 公共字符密码[]=新字符[100]; 公共字符channelName[]=新字符[100]; 公共字符订户[][]=新字符[100][]; 公众国际电话号码; 公共视频; 公共视频[]视频=新视频[100]; 公共静态类视频{ 公共字符串videoId; 公共字符串视频名称; 公共字符串视频持续时间; 公共字符串视频标签; } 公共静态void main(字符串[]args){ //用于初始化 频道[]频道=新频道[100]; 对于(int i=0;i<100;i++){ 通道[i]=新通道(); //通道的设置值
对于(intj=0;j,您可以用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());
    }
}
import java.util.ArrayList;
导入java.util.List;
课堂录像{
字符串videoId;
字符串videoName;
字符串视频持续时间;
字符串视频标签;
}
类频道{
字符串电子邮件;
字符串密码;
字符串通道名;
List subscriber=new ArrayList();
列表视频=新建ArrayList();
public int numberOfSubscriber(){
返回subscriber.size();
}
公共int numberOfVideos(){
返回视频。大小();
}
}
公共班机{
公共静态void main(字符串[]args){
//创建一个新频道
频道=新频道();
//修改一些变量
channel.email=”example@example.com";
频道。订户。添加(“订户1”);
//创建一个新的视频
视频=新视频();
video.videoName=“这是一个视频”;
//将视频添加到频道
频道。视频。添加(视频);
//获取视频的数量
System.out.println(channel.numberOfVideos());
}
}

Java中没有结构,我是说如何使用类来使用它?