Java code: data structures, algorithms, and OOP.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

README.md 2.5 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. # Lists
  2. ## Abstract List Type
  3. The list abstract data type (ADT) implements the following methods:
  4. * size
  5. * isEmpty
  6. * first
  7. * last
  8. * addFirst
  9. * addLast
  10. * removeFirst
  11. Other convenience methods:
  12. * remove
  13. * removeFirst
  14. * removeLast
  15. * removeElement
  16. ## Mistakes Learnings Tricks
  17. Use the `Integer` class when processing integers!
  18. When you have a String and you want an integer:
  19. * Integer.parse("525")
  20. When you have an integer and you want a String:
  21. * Integer.toString(str)
  22. In general, you can also use the String class to turn the object into a String:
  23. * `String.valueOf(o)`
  24. Linked List specific details:
  25. * Need to handle cases where you are adding to an empty list, or removing last item from list
  26. * Especially when using tail pointer, be aware of what it is pointing to and when it needs to be updated.
  27. Linked list methods should automatically become a list of bookkeeping things to do.
  28. * Add method: consider adding to an empty list, non-empty list
  29. * Add at index i:
  30. * What are all possible cases?
  31. * i is too big, i is too small
  32. * the list is empty
  33. * i is a special case
  34. * how can we fold any of these cases into other cases (list empty -> i too small)
  35. Size of linked list:;
  36. * Number of bytes:
  37. * See [https://charlesreid1.com/wiki/Java/Memory](https://charlesreid1.com/wiki/Java/Memory)
  38. * 8 bytes per reference to memory (64 bit)
  39. * 16 bytes of object overhead
  40. * Size estimation:
  41. * 1000-integer linked list should theoretically use up:
  42. * 1000 nodes x ( 4 bytes per integer + 8 bytes per ref + 16 bytes object overhead )
  43. * 28,0000 bytes ~ 28 kB
  44. * What's the power of 2 that would correspond to that?
  45. * 1000 integers ~ 1024 integers ~ 2^10 integers
  46. * Round it up to 8 bytes per integer, 8 bytes per ref, 16 bytes per object overhead)
  47. * 2^5 = 32 bytes per integer
  48. * 32 x 8 = 2^5 * 2^3 = 2^8 = 256 bits = 256 zeroes and ones
  49. * 10 GB / 10 kB/obj = 10x10^6 / 10 = 10^6
  50. Exceptions:
  51. ```
  52. class Empty extends Exception{};
  53. ```
  54. Circular linked list:
  55. * Take care with the details of pointers.
  56. Doubly linked list:
  57. * It makes algorithms easier, but
  58. Nice link explaining sizes of Strings in memory:
  59. * https://stackoverflow.com/a/18030595
  60. ```
  61. sizeof(string) =
  62. 8 + // object header used by the VM
  63. 8 + // 64-bit reference to char array (value)
  64. 8 + string.length() * 2 + // character array itself (object header + 16-bit chars)
  65. 4 + // offset integer
  66. 4 + // count integer
  67. 4 + // cached hash code
  68. ```
  69. Timing:
  70. * Don't do logarithmic size.........
  71. * Need to fix that actually.
  72. * Cover a single order of magnitude, then maybe try for two.