Advertisement
apl-mhd

Remove object reference from array

Dec 28th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. package com.company;
  2.  
  3. import javax.xml.soap.Text;
  4. import java.util.*;
  5.  
  6. public class Main {
  7.  
  8.     public static void main(String[] args) {
  9.  
  10.         ArrayList<Person> personArrayList = new ArrayList<>();
  11.  
  12.         Person arr[] = new Person[4];
  13.         arr[0] = new Person("Apel","36");
  14.         arr[1] = new Person("Sabi","36");
  15.         arr[2] = new Person("Orin","36");
  16.         arr[3] = new Person("Nawsin","36");
  17.  
  18.         personArrayList.add(arr[0]);
  19.         personArrayList.add(arr[1]);
  20.         personArrayList.add(arr[2]);
  21.         personArrayList.add(arr[3]);
  22.  
  23.  
  24.         ArrayList<String> ar = new ArrayList<>();
  25.         ar.add("apel");
  26.         ar.add("Orko");
  27.         ar.add("Orin");
  28.  
  29.         for (Iterator<Person> it = personArrayList.iterator(); it.hasNext();) {
  30.  
  31.             if(it.next().firstName.equals("Apel")) {
  32.                 it.remove();
  33.  
  34.                 System.out.println("remove successfully");
  35.             }
  36.         }
  37.  
  38.         System.out.println(personArrayList.size());
  39.  
  40.         /*for(Person i: personArrayList){
  41.  
  42.             System.out.println(i);
  43.          if(i.firstName.equals("Sabi")){
  44.                    System.out.println("Name found");
  45.                    personArrayList.remove(arr[1]);
  46.  
  47.             }
  48.         }*/
  49. /*
  50.  
  51.  
  52.         System.out.println(personArrayList.size());
  53.  
  54.         System.out.println(Test.arrayList);
  55.  
  56.         Test.arrayList.remove("Apel 1");
  57.  
  58.         System.out.println(Test.arrayList);
  59.  
  60.         Test ob = new Test();
  61.  
  62.         ob.arrayList.add("Nawshin 2");
  63.  
  64.         System.out.println(Test.arrayList);
  65.  
  66.         ob.add("Sabi 36");
  67.         ob.remove(0);
  68.  
  69.         ob.writeToFile();
  70. */
  71.  
  72.  
  73.  
  74.     }
  75. }
  76. package com.company;
  77.  
  78. public class Person {
  79.  
  80.     String firstName;
  81.     String id;
  82.  
  83.     public Person(String firstName, String id) {
  84.         this.firstName = firstName;
  85.         this.id = id;
  86.     }
  87.  
  88.  
  89.     public String toStrsing() {
  90.         return firstName+" "+id;
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement