@@ -44,6 +44,27 @@ public StringList(String[] list) {
44
44
}
45
45
46
46
47
+ /**
48
+ * Construct a StringList from a random pile of objects. Null values will
49
+ * stay null, but all the others will be converted to String values.
50
+ */
51
+ public StringList (Object ... items ) {
52
+ count = items .length ;
53
+ data = new String [count ];
54
+ int index = 0 ;
55
+ for (Object o : items ) {
56
+ // // Not gonna go with null values staying that way because perhaps
57
+ // // the most common case here is to immediately call join() or similar.
58
+ // data[index++] = String.valueOf(o);
59
+ // Keep null values null (because join() will make non-null anyway)
60
+ if (o != null ) { // leave null values null
61
+ data [index ] = o .toString ();
62
+ }
63
+ index ++;
64
+ }
65
+ }
66
+
67
+
47
68
/**
48
69
* Create from something iterable, for instance:
49
70
* StringList list = new StringList(hashMap.keySet());
@@ -113,6 +134,9 @@ public void clear() {
113
134
* @brief Get an entry at a particular index
114
135
*/
115
136
public String get (int index ) {
137
+ if (index >= count ) {
138
+ throw new ArrayIndexOutOfBoundsException (index );
139
+ }
116
140
return data [index ];
117
141
}
118
142
@@ -137,6 +161,22 @@ public void set(int index, String what) {
137
161
}
138
162
139
163
164
+ /** Just an alias for append(), but matches pop() */
165
+ public void push (String value ) {
166
+ append (value );
167
+ }
168
+
169
+
170
+ public String pop () {
171
+ if (count == 0 ) {
172
+ throw new RuntimeException ("Can't call pop() on an empty list" );
173
+ }
174
+ String value = get (count -1 );
175
+ data [--count ] = null ; // avoid leak
176
+ return value ;
177
+ }
178
+
179
+
140
180
/**
141
181
* Remove an element from the specified index.
142
182
*
@@ -274,6 +314,14 @@ public void append(StringList list) {
274
314
}
275
315
276
316
317
+ /** Add this value, but only if it's not already in the list. */
318
+ public void appendUnique (String value ) {
319
+ if (!hasValue (value )) {
320
+ append (value );
321
+ }
322
+ }
323
+
324
+
277
325
// public void insert(int index, int value) {
278
326
// if (index+1 > count) {
279
327
// if (index+1 < data.length) {
@@ -304,12 +352,17 @@ public void append(StringList list) {
304
352
// }
305
353
306
354
355
+ public void insert (int index , String value ) {
356
+ insert (index , new String [] { value });
357
+ }
358
+
359
+
307
360
// same as splice
308
361
public void insert (int index , String [] values ) {
309
362
if (index < 0 ) {
310
363
throw new IllegalArgumentException ("insert() index cannot be negative: it was " + index );
311
364
}
312
- if (index >= values .length ) {
365
+ if (index >= data .length ) {
313
366
throw new IllegalArgumentException ("insert() index " + index + " is past the end of this list" );
314
367
}
315
368
@@ -495,7 +548,7 @@ public void swap(int a, int b) {
495
548
496
549
/**
497
550
* @webref stringlist:method
498
- * @brief To come...
551
+ * @brief Reverse the order of the list elements
499
552
*/
500
553
public void reverse () {
501
554
int ii = count - 1 ;
@@ -592,6 +645,7 @@ public String[] values() {
592
645
}
593
646
594
647
648
+ @ Override
595
649
public Iterator <String > iterator () {
596
650
// return valueIterator();
597
651
// }
@@ -629,7 +683,8 @@ public String[] array() {
629
683
630
684
631
685
/**
632
- * Copy as many values as possible into the specified array.
686
+ * Copy values into the specified array. If the specified array is null or
687
+ * not the same size, a new array will be allocated.
633
688
* @param array
634
689
*/
635
690
public String [] array (String [] array ) {
@@ -679,14 +734,6 @@ public IntDict getOrder() {
679
734
}
680
735
681
736
682
- // public void println() {
683
- // for (int i = 0; i < count; i++) {
684
- // System.out.println("[" + i + "] " + data[i]);
685
- // }
686
- // System.out.flush();
687
- // }
688
-
689
-
690
737
public String join (String separator ) {
691
738
if (count == 0 ) {
692
739
return "" ;
@@ -701,10 +748,11 @@ public String join(String separator) {
701
748
}
702
749
703
750
704
- // static public StringList split(String value, char delim) {
705
- // String[] array = PApplet.split(value, delim);
706
- // return new StringList(array);
707
- // }
751
+ public void print () {
752
+ for (int i = 0 ; i < size (); i ++) {
753
+ System .out .format ("[%d] %s%n" , i , data [i ]);
754
+ }
755
+ }
708
756
709
757
710
758
@ Override
0 commit comments