Advertisement
LoganBlackisle

insertionIntoArray

Jun 30th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. //method
  2. public static void insertCustomer(Customer[] customers, Customer customer) {
  3. Customer cus1 = null;
  4. Customer cus2 = null;
  5. for (int i = 0; i < customers.length - 1; i++) {
  6. if (customers[i] != null) {
  7. if (customers[i].compareTo(customer) < 0 && customers[i + 1].compareTo(customer) > 0) {
  8. for (int j = i + 1; j < customers.length - 1; j++) {
  9. cus1 = customers[j];
  10. cus2 = customers[j + 1];
  11. customers[j] = cus2;
  12. customers[j + 1] = cus1;
  13. }
  14. }
  15. }
  16. }
  17. }
  18.  
  19. //mainapp
  20. public static void main(String[] args) {
  21. Customer[] customers = new Customer[10];
  22. Customer c1 = new Customer("Allan", "Birch", 32);
  23. Customer c2 = new Customer("fn1", "ln1", 35);
  24. Customer c3 = new Customer("fn2", "ln2", 29);
  25. Customer c4 = new Customer("fn3", "ln3", 30);
  26. Customer c5 = new Customer("fn4", "ln4", 31);
  27. Customer c6 = new Customer("fn5", "ln5", 34);
  28. Customer c7 = new Customer("fn6", "ln6", 33);
  29.  
  30. customers[0] = c1;
  31. customers[1] = c2;
  32. customers[2] = c3;
  33. customers[3] = c4;
  34. customers[4] = c5;
  35. customers[5] = c6;
  36. customers[6] = c7;
  37.  
  38. System.out.println();
  39.  
  40. System.out.println("Print array here: " + Arrays.toString(customers));
  41.  
  42. Algoritmer.insertCustomer(customers, c8);
  43.  
  44. System.out.println();
  45.  
  46. System.out.println("Print array here: " + Arrays.toString(customers));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement