从Java数组特定元素的打印数据 [英] printing data of specific element from array in java

查看:321
本文介绍了从Java数组特定元素的打印数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个存储视频类类型(而不是以假乱真只是对象)的数组。
每个视频都有一个标题和作者。
这些视频都使用这样的设置方法来设置自己的数据:

I have an array of a class type which stores videos (not real ones just objects). Each video has a title and an author. The videos have their data set using set methods like these:

 public class Clip {

 private String title;
 private String author;

     public void setTitle (String s1) {

        title = s1;

    }

     public void setAuthor (String s2) {

        title = s2;

    }

然后我有它创建数组,并使用这些设置方法来输入数据的不同的类。
然后,程序要求用户从阵列中选择的元素,例如用户
选择元素[3]。现在,该程序必须打印件(视频)的标题和作者。

I then have a different class which creates the array and uses these set methods to enter data. The program then asks the user to select an element from the array, for example the user selects element [3]. Now the program must print the title and author of that element(video).

这是我不知道该怎么办的一部分吗?
我可以请有一定的帮助?

This is the part I am not sure how to do? Can I please have some help?

推荐答案

您实现裁剪类是错误的。它应该是这样的:

Your implementation of Clip class is wrong. It should be something like this:

public class Clip {
        private String title;
        private String author;

        public Clip(String title, String author) {
            this.title = title;
            this.author = author;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getAuthor() {
            return author;
        }

        public void setAuthor(String author) {
            this.author = author;
        }
    }

和可以从阵列这样的检索您的对象:

And you can retrieve your objects from array like this:

@Test
    public void testClipArray() throws Exception {
        // Lets assume our array contains 2 elements
        Clip[] clipArray = new Clip[2];
        clipArray[0] = new Clip("First", "Clip");
        clipArray[1] = new Clip("Second", "Clip");

        // Lets retrieve 2nd object (with index: 1)
        Clip secondObject = clipArray[1];

        System.out.println(secondObject.getAuthor());
        System.out.println(secondObject.getTitle());
    }

这篇关于从Java数组特定元素的打印数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆