简体   繁体   English

Java。 创建类似于List的新类

[英]Java. Creating new class similar to List

结构体 I would like to create my own class, which had 3 fields. 我想创建自己的类,其中包含3个字段。 The first field - integer, the second would take objects (Strings, Lists), and the third would take integers. 第一个字段为整数,第二个字段为对象(字符串,列表),第三个字段为整数。 I do not understand two things. 我不明白两件事。

  1. How to organize the storage of variables. 如何组织变量的存储。 I need to write a method in which the Array or List will save these values​​? 我需要编写一种方法,其中Array或List将保存这些值吗? How to save in object values? 如何保存对象值?
  2. For second field. 对于第二场。 If the input is a String or a List so what Type is needed? 如果输入是字符串或列表,那么需要什么类型? and if I want to take as primitive types, then what? 如果我想采用原始类型,那又如何呢? How to save object? 如何保存对象?

     public class Record { private int[] number; private int[] count; private Object[] code; public void add(int newNumber, List<String> newCode, int newCount){ return; }; public void add(List<String> newCode, int newCount,){ return; }; 

This doesn't work. 这行不通。

   Object nobj = new Object();
nobj = "ss";

Okay. 好的。 It appears to me that you've misunderstood the purpose of your class. 在我看来,您误解了课堂目的。 You've written a class to simulate a single Record , but you've written Record to store many values. 您已经编写了一个类来模拟单个Record ,但是您编写了Record以存储许多值。

Let's re-arrange your class structure a little 让我们重新安排您的班级结构

public class Record {
private int number;
private int count;
private Object code;

public Record(int number, int count, Object code)
{
     this.number = number;
     this.count = count;
     this.code = code;
}

Then you can create a class, to manage the interface between the Record class, so for example: 然后可以创建一个类,以管理Record类之间的接口,例如:

public class Storage
{
     List<Record> records;
     public Storage()
     {
         this.records = new ArrayList<Record();
     }

     public void addRecord(int number, int count, Object code)
     {
          records.add(new Record(number, count, code));
     }
}

That way, you're not messing about with lots of different arrays, which are horrible to try to keep track of, Everything is neatly wrapped up inside your objects. 这样,您就不会弄乱很多不同的数组,很难跟踪它们。所有内容都整齐地包裹在对象中。

Your issue with the second attribute 您的第二个属性问题

Now, it seems you want to store anything in this variable. 现在,似乎您想在此变量中存储任何内容 This is somewhat more complex than your original problem, but I think that Generics will answer your problem for you. 这比您最初的问题要复杂一些,但是我认为泛型将为您解决问题。 I won't write the code for you, but what I can do is give you a demonstration. 我不会为您编写代码,但是我可以做的是给您演示。

public class GenericExample<T>
{
     T object;

     public GenericExample(T object)
     {
         this.object = object;
     }
}

Okay, so what I've done here is simple. 好的,所以我在这里所做的很简单。 I've created a new class, GenericExample , and I've said that this class has a special type, T . 我创建了一个新类GenericExample ,并且我说过此类有一个特殊类型T This type is defined at run time, and means you can define plenty of different values. 此类型是在运行时定义的,这意味着您可以定义很多不同的值。 For example. 例如。

GenericExample<String> example = new GenericExample<String>("This is a string");
GenericExample<Object> example2 = new GenericExample<Object>(new Object());

See how you can define the type, and pass it in at run time? 看看如何定义类型并在运行时将其传递? Now think about applying it to your class structure. 现在考虑将其应用于您的类结构。

If you really do not need to create your own List , avoid that. 如果您确实不需要创建自己的List ,请避免这种情况。 Just create your own type of data and use it as a parameter for List : 只需创建您自己的数据类型并将其用作List的参数即可:

public class Record {
    private int number;
    private int count;
    private Object code;

    // Constructors, setters and getters
};

List<Record> myList = new ArrayList<>();

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

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