Java. Creating new class similar to List -


structure create own class, had 3 fields. first field - integer, second take objects (strings, lists), , third take integers. not understand 2 things.

  1. how organize storage of variables. need write method in array or list save these values​​? how save in object values?
  2. for second field. if input string or list type needed? , if want take primitive types, what? how 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. appears me you've misunderstood purpose of class. you've written class simulate single record, you've written record store many values.

let's re-arrange class structure 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 can create class, manage interface between record class, 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 lots of different arrays, horrible try keep track of, neatly wrapped inside objects.

issue second attribute

now, seems want store anything in variable. more complex original problem, think generics answer problem you. won't write code you, can give demonstration.

public class genericexample<t> {      t object;       public genericexample(t object)      {          this.object = object;      } } 

okay, i've done here simple. i've created new class, genericexample, , i've said class has special type, t. type defined @ run time, , means can define plenty of different values. example.

genericexample<string> example = new genericexample<string>("this string"); genericexample<object> example2 = new genericexample<object>(new object()); 

see how can define type, , pass in @ run time? think applying class structure.


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

php - MySQLi multi_query results for later use -