Results 1 to 7 of 7
Thread: Generic method problem
- 10-22-2010, 10:31 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
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.
-
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.
- 10-22-2010, 10:48 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
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
-
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:
Also, given your information above, I'm not able to understand or reproduce your problem.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); } }
Luck.
- 10-23-2010, 02:12 AM #5
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
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:
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: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); } } } }
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.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; }
Regards
-
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.
- 10-24-2010, 06:30 PM #7
Member
- Join Date
- Mar 2009
- Posts
- 45
- Rep Power
- 0
Similar Threads
-
Thread problem, calling method in run method
By majk in forum Threads and SynchronizationReplies: 4Last Post: 09-27-2010, 11:40 AM -
Problem with generic attribute in Custom Tag.
By Hans in forum Java ServletReplies: 0Last Post: 08-01-2010, 05:59 PM -
standard input stream storing to a generic method?
By vendetta in forum New To JavaReplies: 3Last Post: 01-29-2010, 08:13 PM -
generic types
By jon80 in forum New To JavaReplies: 6Last Post: 06-12-2009, 10:29 PM -
Calling a method in a different class from within a method problem
By CirKuT in forum New To JavaReplies: 29Last Post: 09-25-2008, 07:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks