diff options
author | RossTheRoss <msattr@gmail.com> | 2019-10-29 11:24:33 -0500 |
---|---|---|
committer | RossTheRoss <msattr@gmail.com> | 2019-10-29 11:24:33 -0500 |
commit | 31a416f7bbcfd2272db0dc5e93fe24cd800d5d5f (patch) | |
tree | e26fc6c40000ade1ee5eaf6f8e98d7a30445e305 /csci1913/Java/notes/sequence.java | |
parent | Make a new generation (diff) | |
download | homework-31a416f7bbcfd2272db0dc5e93fe24cd800d5d5f.tar homework-31a416f7bbcfd2272db0dc5e93fe24cd800d5d5f.tar.gz homework-31a416f7bbcfd2272db0dc5e93fe24cd800d5d5f.tar.bz2 homework-31a416f7bbcfd2272db0dc5e93fe24cd800d5d5f.tar.lz homework-31a416f7bbcfd2272db0dc5e93fe24cd800d5d5f.tar.xz homework-31a416f7bbcfd2272db0dc5e93fe24cd800d5d5f.tar.zst homework-31a416f7bbcfd2272db0dc5e93fe24cd800d5d5f.zip |
Finish things
Diffstat (limited to '')
-rw-r--r-- | csci1913/Java/notes/sequence.java | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/csci1913/Java/notes/sequence.java b/csci1913/Java/notes/sequence.java new file mode 100644 index 0000000..ac8f487 --- /dev/null +++ b/csci1913/Java/notes/sequence.java @@ -0,0 +1,41 @@ +class Sequence<Base> {
+ private Base[] bases;
+ private int count;
+
+ public int find (Base base){
+ if (base == null) {
+ for (int i=0; i<count; i+=1) {
+ if (bases[i] == null) {
+ return index;
+ }
+ }
+ } else {
+ for (int i=0; i<count; i+=1) {
+ if (Base.equals(bases[i])) {
+ return index;
+ }
+ }
+ } return -1; //Fail case
+ }
+//While loop is basically an infinitely nexted series of if statements
+ //(It's called loop unrolling)
+//Sometimes the first execution is special (see below)
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append('[');
+ if (count > 0) {
+ builder.append(bases[0].toString())
+ //Add a helper fo the null pointer
+ for (int i = 1; i < count; i += 1) {
+ builder.append(" ,");
+ //Add a helper to not dereference the null pointer
+ builder.append(bases[i].toString());
+ }
+ } builder.append(']')
+ return builder.toString();
+ }
+//Arrays as sequences
+ //Add O(n) needed a copy loop
+ //Delete O(n) needed a copy loop - first element
+ //Find O(n) used linear search
+ //Can we go faster? (Yes, probably)
\ No newline at end of file |