2
2
3
3
public class DynamicArray <T >{
4
4
//Just a demo.
5
- public static void main (String []a ){
5
+ public static void main (String []a )throws Exception {
6
6
// Could be DynamicArray<Object> ... =new DynamicArray<>();
7
7
DynamicArray <String > dynamics =new DynamicArray <String >();
8
8
dynamics .set (5 , "This is the fifth slot!" );
@@ -12,41 +12,72 @@ public static void main(String[]a){
12
12
System .out .println (
13
13
java .util .Arrays .toString (dynamics .getArray ())
14
14
);
15
- //Can throw ArrayIndexOutOfBounds if not used "properly".
15
+ System .out .println ("But this is how it really looks like:" );
16
+ System .out .println (
17
+ java .util .Arrays .toString (dynamics .getRealArray ())
18
+ );
19
+ //Can throw ArrayIndexOutOfBoundsException if not used "properly".
16
20
System .out .println (dynamics .get (10 ));
17
21
dynamics =new DynamicArray <String >(
18
22
new String []{"Hello?" , "Is" , "It" , "..." }
19
23
);
20
24
System .out .println (
21
25
java .util .Arrays .toString (dynamics .getArray ())
22
26
);
27
+ System .out .println ("But this is how it really looks like:" );
28
+ System .out .println (
29
+ java .util .Arrays .toString (dynamics .getRealArray ())
30
+ );
31
+ System .out .println ("Pre initialized arrays are not automatically expanded until the #get() method is called." );
23
32
}
24
- //Would rather use a List<> to be honest.
25
33
//The class itself
34
+ //It's an empty array, we're not affecting any data at all.
35
+ @ SuppressWarnings ("unchecked" )
36
+ T [] underlyingArr =(T [])new Object [0 ];
37
+ int size =0 ;
38
+ int actualSize =0 ;
26
39
public DynamicArray (){}
27
40
public DynamicArray (final T [] newArr ){
28
41
underlyingArr =newArr ;
42
+ actualSize =newArr .length ;
43
+ size =newArr .length ;
29
44
}
30
- //T is an Object, don't mind it too much.
31
- //Also, it's an empty array, we're not affecting any data at all.
32
- @ SuppressWarnings ("unchecked" )
33
- T [] underlyingArr =(T [])new Object [0 ];
34
45
public void set (final int ind , final T value ){
35
- if (underlyingArr . length <ind +1 ){
46
+ if (actualSize <ind +1 ){
36
47
//It's an empty array, we're not affecting any data at all.
37
48
@ SuppressWarnings ("unchecked" )
38
- final T [] duplicate =(T [])new Object [ind +1 ];
49
+ final T [] duplicate =(T [])new Object [ind +10 ];
39
50
System .arraycopy (
40
- underlyingArr , 0 , duplicate , 0 , underlyingArr . length
51
+ underlyingArr , 0 , duplicate , 0 , actualSize
41
52
);
53
+ actualSize =ind +10 ;
42
54
underlyingArr =duplicate ;
43
55
}
56
+ if (size <ind +1 )
57
+ size =ind +1 ;
44
58
underlyingArr [ind ]=value ;
45
59
}
46
- public T get (final int ind ){
60
+ public T get (final int ind )throws ArrayIndexOutOfBoundsException {
61
+ if (ind >size -1 )
62
+ throw new ArrayIndexOutOfBoundsException (
63
+ new StringBuilder ("Index " )
64
+ .append (String .valueOf (ind ))
65
+ .append (" out of bounds for length " )
66
+ .append (size )
67
+ .toString ()
68
+ );
47
69
return underlyingArr [ind ];
48
70
}
71
+ public T [] toArray (){
72
+ return getArray ();
73
+ }
49
74
public T [] getArray (){
75
+ @ SuppressWarnings ("unchecked" )
76
+ final T [] returnMe =(T [])new Object [size ];
77
+ System .arraycopy (underlyingArr , 0 , returnMe , 0 , size );
78
+ return returnMe ;
79
+ }
80
+ public T [] getRealArray (){
50
81
return underlyingArr ;
51
82
}
52
83
}
0 commit comments