8 Replies - 14637 Views - Last Post: 22 April 2012 - 03:46 PM Rate Topic: -----

#1 overrated  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-October 11

Adding elements from one class to an arraylist from another class

Posted 19 April 2012 - 01:50 PM

I'm not quite sure on how to add elements from one class to another class that contains the arraylist.

For example dog class would have:
 String dog = "Cool";
 String dog1 = "Awesome";
 String dog2 = "Pawsome";



And the doggysay class would have:
 ArrayList<String>doggy;



How do I add the elements from the dog class to the doggysay class?

Is This A Good Question/Topic? 0
  • +

Replies To: Adding elements from one class to an arraylist from another class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding elements from one class to an arraylist from another class

Posted 19 April 2012 - 01:55 PM

The Dog class can create an instance of DoggySay object
The DoggySay class can have a method addToList(String str)
that adds the STring received as parameter to the ArrayList
Was This Post Helpful? 0
  • +
  • -

#3 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Adding elements from one class to an arraylist from another class

Posted 19 April 2012 - 02:02 PM

That really depends. Suppose I have a dog class storing the name of the dog, e.g.

public class Dog{ //This is stored in a .java file called Dog
	private string dogName;
	
	public Dog(String name){
		this.dogName = name;
	}
        
        //Other methods related to dog
}



If I want to add instances of this class in an Arraylist, which is located at the DoggySay class, you need to make a method addToList (just as pbl says). This would look ike:

public class DoggySay{
	ArrayList<Dog> dogsList;	//Reserve an ArrayList to store the dogs (?)
	
	//Constructor. This should instantiate the dogsList
	public DoggySay(){ 
	}
	
	public void addToList(String dogName){
		//Create a new Dog object 
		//Put the Dog object in the ArrayList called dogsList. Use the .add() method for this
	}
	
}



I hope this clarifies it ^^

Edit: reinterpreted the question

This post has been edited by karabasf: 19 April 2012 - 02:14 PM

Was This Post Helpful? 0
  • +
  • -

#4 overrated  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-October 11

Re: Adding elements from one class to an arraylist from another class

Posted 19 April 2012 - 03:21 PM

Thanks guys it does help me get a start of what I need to begin with.

Since there is 3 strings in the dog class would that mean I would need a loop to keep on looping the strings into the ArrayList?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Adding elements from one class to an arraylist from another class

Posted 19 April 2012 - 08:36 PM

You can loop if they are declared like this
String dog = "Cool";
String dog1 = "Awesome";
String dog2 = "Pawsome";


but you can if they are declared that way
String[] appeciation = {"Cool", "Awesome", "Pawsome"};


Was This Post Helpful? 0
  • +
  • -

#6 overrated  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-October 11

Re: Adding elements from one class to an arraylist from another class

Posted 22 April 2012 - 04:26 AM

Sorry, I'm not quite sure how. So far I have:

public class dog {
DoggySay myDog = new DoggySay();

String name = myDog.addname("Bow Wow");
String[] appreciation = {"Cool", "Awesome", "Pawsome"};
}



public class DoggySay {
private String dog_name;
ArrayList<dog> love_doggy;

public DoggySay()
{
    dog_name = "";
}
public String addname(String dname)
{
    dog_name = dname;
    return dog_name;
}
public void addAppreciation() //Don't know if I did this right.
{
    //The part I need help on.
}

}



For
 public void addAppreciation 
, I don't know if I should use void or String. Also is it possible to say
public void addAppreciation(String[] dappreciation)
and get the dappreciation from the dog class, String[] appreciation = {"Cool", "Awesome", "Pawsome"}; and add it to an arraylist?
Was This Post Helpful? 0
  • +
  • -

#7 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Adding elements from one class to an arraylist from another class

Posted 22 April 2012 - 04:54 AM

Quote

For
 public void addAppreciation 
, I don't know if I should use void or String.


That depends, do you want to return a string when you add something? If not, a void is ok. Another common construction is to return a boolean for the add method. If the add was succesful, return true; false if otherwise.

Quote

Also is it possible to say
public void addAppreciation(String[] dappreciation)
and get the dappreciation from the dog class, String[] appreciation = {"Cool", "Awesome", "Pawsome"}; and add it to an arraylist?


Yes, this is possible. Just loop all the elements of the string array, e.g.

public void addAppreciation(String[] dappreciation)
for(int i = 0; i < dappreciation.length; i++)
   love_doggy.add(dappreciation[i]); //Add the current element to the love_doggy array list



Good luck ;)
Was This Post Helpful? 0
  • +
  • -

#8 overrated  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-October 11

Re: Adding elements from one class to an arraylist from another class

Posted 22 April 2012 - 02:07 PM

I do want to return a string to my dog class. New java I have is below and I am getting some errors that I tried to fix but doesn't work.

public class dog {
DoggySay myDog = new DoggySay();

String name = myDog.addname("Bow Wow");
String[] appreciation = {"Cool", "Awesome", "Pawsome"};

for(int i = 0; i < appreciation.length; i++)
{
    String doggy_appreciation = myDog.addAppreciation(appreciation[i]); //Getting error. Tried String doggy_appreciation = myDog.addAppreciation[i] and myDog.addAppreciation[i]
}

System.out.println(name);
System.out.println(doggy_appreciation);
}




public class DoggySay {
private String dog_name;
ArrayList<dog> love_doggy;

public DoggySay()
{
    dog_name = "";
}
public String addname(String dname)
{
    dog_name = dname;
    return dog_name;
}
public String addAppreciation(String[] dappreciation) 
{
    for(int i = 0; i < dappreciation.length; i++)
    {
        love_doggy.add(dappreciation[i]); //Error over here
    }
    return love_doggy; //Error over here too
}

}



Was This Post Helpful? 0
  • +
  • -

#9 overrated  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 29-October 11

Re: Adding elements from one class to an arraylist from another class

Posted 22 April 2012 - 03:46 PM

Thanks for ya'll help. I have figured it out :bigsmile:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1