1 2 Previous Next 16 Replies Latest reply: Sep 1, 2011 7:44 AM by tschodt RSS

    Allow only String to List

    698065
      Hello,

      I have a ArrayList.
      ArrayList as = new ArrayList();
      Now i want to allow only string to be added in this arraylist how can i achieve this? Don't want to use generic.

      Please guide.
        • 1. Re: Allow only String to List
          796440
          Why not use generics?

          If you are insisting on not using generics, then use the Decorator pattern to wrap the List with your own implementation that delegates everything to the wrapped List but throws an exception on any call that tries to insert a non-String.
          • 2. Re: Allow only String to List
            802316
            I would start with
            List<String> as = new ArrayList<String>();
            http://download.oracle.com/javase/tutorial/java/generics/index.html
            • 3. Re: Allow only String to List
              836548
              Ankur Raiyani wrote:
              Hello,

              I have a ArrayList.
              ArrayList as = new ArrayList();
              Now i want to allow only string to be added in this arraylist how can i achieve this? Don't want to use generic.
              If you don't want to use generic, then use instanceof operator.

              for example
              ArrayList as = new ArrayList();
              String a = new String("XXX");
              if(a instanceof String)
               as.add(a);
              • 4. Re: Allow only String to List
                698065
                i have been asked to do not use generic.

                I want such a way that List only allows me to add String if i add other object then it should throw an error.
                • 5. Re: Allow only String to List
                  darke
                  Ankur Raiyani wrote:
                  i have been asked to do not use generic.

                  I want such a way that List only allows me to add String if i add other object then it should throw an error.
                  Create a subclass nad override the relevant methods in the ArrayList to suit your requirements .
                  • 6. Re: Allow only String to List
                    EJP
                    i have been asked to do not use generic.
                    Why?
                    I want such a way that List only allows me to add String if i add other object then it should throw an error.
                    So somebody actually prefers a runtime exception to a compile time error?

                    Why?

                    Is this homework?
                    • 7. Re: Allow only String to List
                      836548
                      Ankur Raiyani wrote:
                      i have been asked to do not use generic.
                      In given example where you have seen generics. It is clearly said if you don't want to use generics then use instanceof keyword.
                      I want such a way that List only allows me to add String if i add other object then it should throw an error.
                      Exactly , i've gave an idea how to use instanceof and allow only string objects in arraylist.

                      If not clear, please go through the below program:
                      import java.util.*;
                      import java.lang.*;
                      class ArrayListForOnlyString 
                      {
                           
                           public static void main(String st[])
                           {
                                List stringArrayList = new ArrayList(){
                                
                                
                                     public ArrayList ar = new ArrayList();
                           
                                     public boolean add(Object ob) throws UnsupportedOperationException
                                     {
                                          if(ob instanceof String)
                                          {
                                               ar.add(ob);
                                               return true;
                                          }     
                                          else
                                          throw new UnsupportedOperationException("Only String Object are accepted");
                                     }
                                     
                                     public void add(int index, Object ob)
                                     {
                                          if(ob instanceof String)
                                          {
                                               ar.add(index,ob);
                                          }
                                          else                    
                                          throw new UnsupportedOperationException("Only String Object are accepted");
                                     }
                                     
                                     public Object[] toArray()
                                     {
                                          return ar.toArray();
                                     }
                                     
                                     public String toString()
                                     {
                                          return ar.toString();
                                     }
                                
                                };
                                
                                try
                                {
                                
                                stringArrayList.add(new String("10"));
                                stringArrayList.add(1,new String("10"));
                                      // Uncomment the below line and run, it 'll give error , as you are trying to add Integer Object to array list.
                                //stringArrayList.add(new Integer("10"));
                                System.out.println("The objects of arrayList are : "+stringArrayList);
                                
                               }catch(Exception e)
                                     {
                                          e.printStackTrace();
                                     }
                           }
                      
                      }
                      Edited by: 833545 on Aug 31, 2011 3:53 PM
                      • 8. Re: Allow only String to List
                        EJP
                        You can do all that without the inner ArrayList, as you're already extending one. Remove the declaration of 'ar' and replace all the other uses by 'super'. That also saves you from having to override toArray() and toString().

                        Why the requirement remains a mystery.
                        • 9. Re: Allow only String to List
                          tschodt
                          Ankur Raiyani wrote:
                          I have a ArrayList.
                          ArrayList as = new ArrayList();
                          Now i want to allow only string to be added in this arraylist how can i achieve this? Don't want to use generic.

                          Please guide.
                          Peruse java.util javadoc...

                          [url http://download.oracle.com/javase/6/docs/api/java/util/Collections.html#checkedList%28java.util.List,%20java.lang.Class%29]List Collections.checkedList(List lst, Class cls)
                          • 10. Re: Allow only String to List
                            698065
                            Thanks.
                            • 11. Re: Allow only String to List
                              796440
                              Ankur Raiyani wrote:
                              i have been asked to do not use generic.

                              I want such a way that List only allows me to add String if i add other object then it should throw an error.
                              And I already told you how to do that, in the very first response.
                              • 12. Re: Allow only String to List
                                796440
                                833545 wrote:
                                If not clear, please go through the below program:
                                1) Don't provide full code solutions. It is not helpful.

                                2) IllegalArgumentException is more appropriate than UnsupportedOperationException.

                                3) Returning the result of the underlying List's add() call would be more appropriate than always returning true.
                                • 13. Re: Allow only String to List
                                  796440
                                  EJP wrote:
                                  So somebody actually prefers a runtime exception to a compile time error?
                                  And prefers to have an apparently very junior developer reinvent the wheel in code over using a language feature.

                                  I pray this is for a class and not a job.
                                  • 14. Re: Allow only String to List
                                    698065
                                    Yes this is for only class not for a job.

                                    Thanks.
                                    1 2 Previous Next