Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. public class UnorderedArrayList<T> extends ArrayListClass<T> {
  2. public UnorderedArrayList(){
  3. super();
  4. }
  5. public UnorderedArrayList(int size){
  6. super(size);
  7. }
  8. public void insertAt(int loc, T insertItem){
  9. if(loc < 0 || loc >= maxSize){
  10. System.err.println("Entered index out of range.");
  11. }
  12. else if(length >= maxSize){
  13. System.err.println("List is full!");
  14. }
  15. else{
  16. for(int i = length; i > loc; i--){
  17. list[i] = list[i - 1];
  18. }
  19. list[loc] = insertItem;
  20. length++;
  21. }
  22. }
  23. public int seqSearch(T searchItem){
  24. int loc;
  25. boolean found = false;
  26. for(loc = 0; loc < length; loc++){
  27. if(list[loc].equals(searchItem)){
  28. found = true;
  29. break;
  30. }
  31. }
  32. if(found){
  33. return loc;
  34. }
  35. else{
  36. return -1;
  37. }
  38. }
  39. public void remove(T removeItem){
  40. int loc;
  41. if(length == 0){
  42. System.err.println("List is empty!");
  43. }
  44. else{
  45. loc = seqSearch(removeItem);
  46. if(loc != -1){
  47. removeAt(loc);
  48. }
  49. else{
  50. System.out.println("The item to be deleted is not in the list!");
  51. }
  52. }
  53. }
  54. public void replaceAt(int loc, T replaceItem){
  55. if(loc < 0 || loc >= length){
  56. System.err.println("Entered index is out of range");
  57. }
  58. else{
  59. list[loc] = replaceItem;
  60. }
  61. }
  62. public void insertEnd(T insertItem){
  63. if(length >= maxSize){
  64. System.err.println("The list is full!");
  65. }
  66. else{
  67. list[length] = insertItem;
  68. length++;
  69. }
  70. }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement