Simple example of custom linear table

xiaoxiao2021-03-06  15

/ * * Create Date 2005-2-5 * * Todo To change the template for this generated file, go to * Window - Preference - Java - Code Style - Code Template * /

/ ** * @Author bitan * * Todo To change this generated type annotation template, go to * Window - Preference - Java - Code Style - Code Template * / Public Class Test4 {Class Digital {Private Final Int Intesize; private final int increase; private Object [] arrays; private int items; private int capacity; public void removeAll (int offset, int length) {if (offset <0 || length <0 || (offset length)> items) Throw New IllegalargumentException ("Arguments May Be Illegal."); items - = length; Capacity - = ((Capacity -Items) / Increase) * Increase; Object [] Temps = New Object [Capacity]; for (int i = 0 i Capacity) {Capacity = ((items - capacity)) / increase 1) * increase;} Object [] temps = new object [Capacity ]; For (int i = 0; i

} For (int i = index, j = 0; j this.items) {throw new illegalargumentException ("Arguments May Be illegal.");} returnid.Arrays [index] ;

} Public Object getFirstElement () {return arrays [0];} public Object getLastElement () {return arrays [items-1];} public void removeLastElement () {removeElement (items - 1);} public void removeFirstElement () {removeElement (0);} public void removelelement (int index) {IF (INDEX <0 || index> this.items) {throw new IllegalargumentException ("Arguments May Be Illegal.");} While (index

public void insertElement (int index, Object o) {if (o == null || index <0 || index> items) throw new IllegalArgumentException ( "Argument may be illegal."); items ; if (items> capacity) capacity = increase; object [] tempacity]; for (int i = 0; i

Public static void main (string [] args) {test4 t4 = new test4 (); DIGITAL D = T4.NEW DIGITAL (3, 2); DIGITAL D2 = T4.NEW Digital (6, 3); string [] SARR = New string [8]; for (int i = 0; i

result:

TEST4 $ DIGITAL: (SIZE = 4, Capacity = 5) [BBBB0, BBBB1, BBBB2, BBBB3] TEST4 $ DIGITAL: (size = 8, Capacity = 9) [AAAA0, AAAA1, AAAA2, AAAA3, AAAA4, AAAA5, AAAA6, AAAA7] TEST4 $ DIGITAL: (SIZE = 12, Capacity = 15) [AAAA0, AAAA1, BBBB0, BBBB1, BBB2, BBBB3, AAAA2, AAAA3, AAAA4, AAAA5, AAAA6, AAAA7] TEST4 $ DIGITAL: (size = 8, Capacity = 9) [AAAA0, AAAA1, BBBB2, BBBB1, BBB2, AAAA5, AAAA6, AAAA7] first = aaaa0, last = aaaa7, index [5] = aaaa5

TEST4 $ DIGITAL: (size = 9, capacity = 9) [AAAA0, AAAA1, BBBB2, BBBB1, BBBB2, AAAA5, AAAA6, CCCC0, AAAA7]

in conclusion:

First, when designing the linear table, the most complicated is to increase and delete the method according to the index, the simplest is the search method.

Second, when implementing a method similar to a similar function, a method of index parameters should be achieved. Then, the method without the index parameters can simply call the method of index parameters.

转载请注明原文地址:https://www.9cbs.com/read-46486.html

New Post(0)