There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
help me with ArrayList method addAll


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
virkjay's Avatar
Junior Member with 10 posts.
 
Join Date: Nov 2007
Experience: Beginner
01-Nov-2007, 11:55 AM #1
help me with ArrayList method addAll
Hi guys, I am trying to wrtie a method to add all elements of one ArrayList to another one. My code is
import java.util.*;
public class Purse
{
public Purse()
{
coins = new ArrayList<String>();
}
public void add(String coinName)
{
coins.add(coinName);
}
public String toString()
{
if(coins.size() == 0)
return "Purse[]";
String output = "Purse[";
for(String coin : coins)
{
output = output + coin + ",";
}
return output + "]";
}
public void transfer1(Object other)
{
Purse otherPurse = (Purse) other;
coins.addAll(otherPurse);
other = null;
}

private ArrayList<String> coins;
}

The error i got is Purse.java:41: cannot find symbol
symbol : method addAll(Purse)
location: class java.util.ArrayList<java.lang.String>
coins.addAll(otherPurse);
^
1 error

Tool completed with exit code 1

when i tried to do in simple way it is perfectly work which is

import java.util.*;
public class TransferArrayList
{
public static void main(String args[])
{
ArrayList<String> list = new ArrayList();
list.add("abc");
list.add("fda");
ArrayList<String> list1 = new ArrayList();
list1.addAll(list);
System.out.print(list1);
list.clear();
System.out.println(list);
}

}

Can some one please tell me where i m doing mistake.
Chicon's Avatar
Computer Specs
Distinguished Member with 6,608 posts.
 
Join Date: Jul 2004
Location: 50° 34' 07.13" N - 04° 10' 23.
Experience: Second socks retriever
01-Nov-2007, 12:55 PM #2
Hi virkjay,

Welcome to TSG !

The following method is incorrect :
Code:

    public void transfer1(Object other) {
        Purse otherPurse = (Purse) other;
        coins.addAll(otherPurse);
        other = null;
    }
- the casting of Purse is simply wrong : Purse is not an ArrayList
- you may pass an Object as parameter to the method but you must test if the object is an instance of ArrayList<String>

BTW, it is better to respect the data type of the attribute you want to update

The following coding is more appropriate :

Code:

    public void transfer1(ArrayList<String> other) {
        coins.addAll(other);
    }
virkjay's Avatar
Junior Member with 10 posts.
 
Join Date: Nov 2007
Experience: Beginner
01-Nov-2007, 06:34 PM #3
Thanks
Thanks mate, it is realy helpful.
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:51 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.