简体   繁体   中英

Java. Creating new class similar to List

结构体 I would like to create my own class, which had 3 fields. 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​​? 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.

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:

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.

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 . 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. Just create your own type of data and use it as a parameter for List :

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

    // Constructors, setters and getters
};

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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