Wednesday 28 January 2015

How do I remove objects from an array in Java

How do I remove objects from an array in Java


I have array of object type how to remove the object from it

i have class StockExchange 
     and i have array of the same class

  StockExchange[] t= new StockExchange[]{TSE,HKSE,NYSE};

now i want to remove the element from this array 

Here is full example how i removed.
-----------------------------------------------------------------------------
class StockExchange{
    public boolean isClosed() {
return false;
}
--------------------------------------------------------------------------------
public class ObjectRemovalTest {
  
    public static void main(String args[])
    {
    StockExchange TSE = new StockExchange(){
            
            @Override
            public boolean isClosed() {
                return true;
            }         
       };
     
       StockExchange HKSE = new StockExchange(){

            @Override
            public boolean isClosed() {
                return true;
            }         
       };
      
       StockExchange NYSE = new StockExchange(){

            @Override
            public boolean isClosed() {
                return false;
            }         
       };
      

       
    StockExchange[] t= new StockExchange[]{TSE,HKSE,NYSE};
        System.out.println("T"+Arrays.toString(t));
        Set<StockExchange> asSet = new HashSet<StockExchange>(Arrays.asList(t));                   asSet.remove(TSE);
        t = asSet.toArray(new StockExchange[] {});
        System.out.println(Arrays.toString(t));
   
    }

No comments:

Post a Comment