Results 1 to 7 of 7
  1. #1
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default Generic method problem

    Hi creating a generic method which takes an arraylist of either integers or strings and an integer.

    In the main method I have used integer objects to put in the first array list and it works fine in the method.

    However when I try and put a second arraylist of strings I get a error message when I compile.

    Heres my code:

    public static <T> ArrayList<T> arrayConst(ArrayList<T> a,T n){
    ArrayList<T> b = new ArrayList<T>();

    for(int i=0;i<a.size();i++){
    b.add(a.get(i));
    }

    for(int i=0;i<b.size();i++){
    if(b.get(i).equals(n)){
    b.remove(i);
    }
    }

    return b;
    }

    The arraylist i try to put in the method is this :

    ArrayList<String> b = new ArrayList<String>();
    b.add(0,"1");
    b.add(1,"2");
    b.add(2,"3");

    Have I written my generic method wrong or am I missing something basic?

    thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You may wish to reply to help given to you in your previous threads:

    checking a palindrome sentance
    Checking ascending order of array

    Before asking a new question. At least acknowledging that you read them and appreciate the time and effort that someone put in answering it would be nice to see.

    Luck.

  3. #3
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    Yeah sorry I thought I already messaged people for thanks on helping, as I do usally do it whenever I get help or guidence.

    Regards

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Thanks.

    Could you define your problem better? Also, it would help immensely for you to use code tags when posting code and to give us a small compilable version of your problem. For instance, check how much nicer code looks with code tags:

    Java Code:
    import java.util.ArrayList;
    
    public class ArrayListTest {
        public static <T> ArrayList<T> arrayConst(ArrayList<T> a, T n) {
            ArrayList<T> b = new ArrayList<T>();
    
            for (int i = 0; i < a.size(); i++) {
                b.add(a.get(i));
            }
    
            for (int i = 0; i < b.size(); i++) {
                if (b.get(i).equals(n)) {
                    b.remove(i);
                }
            }
    
            return b;
        }
    
        public static void main(String[] args) {
            
            
            ArrayList<Integer> a = new ArrayList<Integer>();
            a.add(0, 1);
            a.add(1, 2);
            a.add(2, 3);
            
            ArrayList<Integer> fooA = arrayConst(a, 2);
            System.out.println(fooA);
            
            
            ArrayList<String> b = new ArrayList<String>();
            b.add(0, "1");
            b.add(1, "2");
            b.add(2, "3");
            
            ArrayList<String> foo = arrayConst(b, "2");
            System.out.println(foo);
            
        }
    }
    Also, given your information above, I'm not able to understand or reproduce your problem.

    Luck.

  5. #5
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    Thanks for your reply,

    Pretty much I'm trying to answer a question about generic methods what I needed to do first was make a method which could take an integer array list and user enter numbers into it. From that the user would select another number where the method would remove this number if it was in the array list. Ive managed to make one that works so far for just creating an integer arraylist, code is here:

    Java Code:
    import java.util.*;
    
    class quest9
    {
     
     public static void main(String[] args)
     {
    
      Scanner input = new Scanner(System.in);
      System.out.println("Enter integers for the ArrayList");
      String line = input.nextLine();
      String[] numbers = line.split(" +");
      ArrayList<Integer> a = new ArrayList<Integer>();
      for(int i=0; i<numbers.length; i++)
          a.add(new Integer(numbers[i]));
    
    System.out.println("Enter the number to be removed in the ArrayList: ");
    String tar = input.nextLine();
      int target = Integer.parseInt(tar);
      
    
      arrayDest(a,target);
      System.out.println("");
      System.out.println("Heres the values of ArrayList a : " + a);
    
    
    }
    
    public static void arrayDest(ArrayList<Integer> a,int n){
    	for(int i=0;i<a.size();i++){
    		if(a.get(i).equals(n)){
    			a.remove(i);
    		}
    	}
    
    
    
    }
    
    }
    This works fine. However I now need to create a generic method of this where it could also use an arraylist or strings instead of integers. Ive made this:

    Java Code:
    public static <T> ArrayList<T> arrayConst(ArrayList<T> a,T n){
    ArrayList<T> b = new ArrayList<T>();
    
    for(int i=0;i<a.size();i++){
    	b.add(a.get(i));
    }
    	
    	for(int i=0;i<b.size();i++){
    		if(b.get(i).equals(n)){
    			b.remove(i);
    		}
    	}
    
    return b;
    }
    but whenever I run this with an arraylist of strings as an parameter i get a error from the complier, but it will compile with an arraylist of integers.

    Regards

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to show us a compilable version of this and show us the error message. When I run my code that I posted above, I get no error messages.

  7. #7
    counterfox is offline Member
    Join Date
    Mar 2009
    Posts
    45
    Rep Power
    0

    Default

    I have tried your code and it works how I want it to. Thanks alot for the help

Similar Threads

  1. Thread problem, calling method in run method
    By majk in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-27-2010, 12:40 PM
  2. Problem with generic attribute in Custom Tag.
    By Hans in forum Java Servlet
    Replies: 0
    Last Post: 08-01-2010, 06:59 PM
  3. standard input stream storing to a generic method?
    By vendetta in forum New To Java
    Replies: 3
    Last Post: 01-29-2010, 09:13 PM
  4. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 11:29 PM
  5. Replies: 29
    Last Post: 09-25-2008, 08:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •