The younger brother is a Java, meet a problem,
How to define a class inheritance in an array?
Because:
1, when the program code, need to use subscript direct access to the elements, such as the class defines a Row, the Row is a Row of object, and then through the Row [3] can direct access to the elements of the value,
2, can realize the dynamic adding elements and delete elements
I think the easiest way is to write a class, and then reload the [] operator, but the Java does not support, then want to write a class can inherit from an array? Study for a few days, did not find a solution, so refer to everyone!
Thank you very much!
I don't use ArrayList, because it cannot access elements, by [] only. The get () method,
Ask you for help again! Thank you very much!
CodePudding user response:
You directly to set the Row type array is not good? Why bother with inheritance in an array, a Java would not support operator overloadingFor example,
The Row Row []=new Row [10].//directly with not so? Have to use a Row to achieve array and what special significance?
The row [0]=XXX;
CodePudding user response:
@ qybao because you such a definition is not dynamically add and remove, I want to extend the function of the array, so that it not only supports the subscript direct access, you can also dynamically add and remove,CodePudding user response:
The get () to access the bottom have what not good he is a dynamic array to achieveCodePudding user response:
Write a List array method, not just solve the problem?CodePudding user response:
With an array as can dynamically extendWrite a method such as
Row, Row [] the resize (Row [] int size) {
Row [] newrow=new Row [size];//to regenerate the array
System. Arraycopy (newrow row, 0, 0, row length);//copy the old array into the new array
Return newrow;
}
When using the
The Row Row []=new Row [10].
for(int i=0; i<10; I++) {
Row [I]=new row ();
}
//extend array
The row=the resize (row, 20);//retained the original 10 Row object