6 Replies - 741 Views - Last Post: 23 April 2011 - 12:13 PM Rate Topic: -----

#1 skyerz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Objects repeating in arrayList

Posted 23 April 2011 - 10:02 AM

ok i have a silly issue whihc i dont understand, the following code i done recieve an error however in the println statemnet i recieve duplicates, when i cancel out the toString method whihc was overidden this is my result
[[email protected]]
[[email protected], [email protected]]
[[email protected], [email protected], [email protected]]

i believe this is showing the memory location of the object, and i have noticed each new value in turn overwrites the previous, could some please explain why it is doing this and what code i should use to overcome this
import java.util.*;
import java.io.*;
abstract class Animal {
	static String name;
	static String cname;
	String noise;
	static ArrayList<Animal> al= new ArrayList<Animal>(); ;
	
	
	public Animal(String n)// seperaate constructor
	
	{
		name = n;
	}
	public static void main (String[] args)throws IOException
	{
		Dog d = new Dog("fido");
		System.out.print(d.name);
		//System.out.println(d.name);
		Cat c = new Cat("buju");
		System.out.println(" "+ d.get_noise());
	
		;
		FileReader fr = new FileReader("cat.txt");
		BufferedReader br = new BufferedReader(fr);
		String line;
		while((line = br.readLine())!=null)
		{
			if (line.startsWith("Cat"))
			{
				String name1 = line.substring(line.indexOf("Cat")+4, line.length());
				//Cat c = new Cat(name1);
				
				//al.add(new Cat(name1));
				Animal n = new Cat(name1);
				al.add(n);
				
			
				System.out.println(al);
				 
				
			}
		}
		/*for (Animal p :al)
		{
			System.out.println(p.name +" "+p.noise);
		}*/
		
		
	}
	/*public String toString()
	{
		return name +" " + noise;
	}*/
	
	
	public abstract String get_noise();
	
	
	
}

	



Is This A Good Question/Topic? 0
  • +

Replies To: Objects repeating in arrayList

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3744
  • View blog
  • Posts: 17,121
  • Joined: 20-September 08

Re: Objects repeating in arrayList

Posted 23 April 2011 - 11:12 AM

Quote

and i have noticed each new value in turn overwrites the previous, could some please explain why it is doing this and what code i should use to overcome this


None of your variables should be marked static. That's your problem: you need to read up on what static means
Was This Post Helpful? 0
  • +
  • -

#3 exiles.prx   User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 241
  • Joined: 22-November 10

Re: Objects repeating in arrayList

Posted 23 April 2011 - 11:13 AM

I am not sure what the issue is here. You are adding a new Cat object to the list and then printing out the address of every object in the list. On each iteration of the loop, you are adding a new Cat object and printing out the address of every object within the list. So with each iteration, your output will show the address of the previously added objects as well as the newly added object.
Was This Post Helpful? 0
  • +
  • -

#4 skyerz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: Objects repeating in arrayList

Posted 23 April 2011 - 11:26 AM

the actual output is this
[Tiddles meow]
[Tabby meow, Tabby meow]
[Solomon meow, Solomon meow, Solomon meow]
ideally i wanted to only print the result once however i tried putting the print statement outside the loop and it printed solomon four times
Was This Post Helpful? 0
  • +
  • -

#5 exiles.prx   User is offline

  • D.I.C Head

Reputation: 65
  • View blog
  • Posts: 241
  • Joined: 22-November 10

Re: Objects repeating in arrayList

Posted 23 April 2011 - 11:36 AM

If you only want to output the object which was just added, you have to use al.get(index) where index is the index of the recently added object.

But like goose said, your variables shouldn't be marked static.
Was This Post Helpful? 1
  • +
  • -

#6 skyerz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: Objects repeating in arrayList

Posted 23 April 2011 - 11:41 AM

i have change them from static but i got the same result as before, i will try using the index, however im sure that you shouldn't need to do this in order to print out the information.
Was This Post Helpful? 0
  • +
  • -

#7 skyerz   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 211
  • Joined: 14-October 10

Re: Objects repeating in arrayList

Posted 23 April 2011 - 12:13 PM

for some odd reason it works now, could be due to using static , i changet the whole code interface and now it works
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


class Cat implements Animal
{
	String name;
	String noise;
	
	public Cat(String n) {
		name = n;
		noise = "meow";
	}

	public String get_noise()
	{
		return noise;
	}
	public String get_name()
	{
		return name;
	}
	public String toString()
	{
		return name +" "+ noise;
	}
	
	public static void main (String[] args)throws IOException
	{
		Dog d = new Dog("fido");
		System.out.print(d.name);
		//System.out.println(d.name);
		Cat c = new Cat("buju");
		System.out.println(" "+ d.get_noise());
	
		;
		FileReader fr = new FileReader("cat.txt");
		BufferedReader br = new BufferedReader(fr);
		String line;
		while((line = br.readLine())!=null)
		{
			int index = 0;
			if (line.startsWith("Cat"))
			{
				String name1 = line.substring(line.indexOf("Cat")+4, line.length());
				//Cat c = new Cat(name1);
				
				//al.add(new Cat(name1));
				Animal n = new Cat(name1);
				al.add(n);
				
			
				//System.out.println(al);
				//System.out.println(index);
				index++;
				 
				
			}
			
			if (line.startsWith("Dog"))
			{
				
				//System.out.println("dog index "+ index);
			}
		}
		System.out.println(al);
		// if(animal is instance of cat only say its name 
		
	}
	//String noise;

	
}
	


import java.util.*;
import java.io.*;
interface Animal {
	
	static ArrayList<Animal> al= new ArrayList<Animal>(); ;
	
	public String get_name();
	

	
	/*public String toString()
	{
		return name +" " + noise;
	}*/
	
	
	public String get_noise();
	
	
	
}

	


class Dog implements Animal
{
	String noise;
	String name;
public Dog(String c)
{
	
	noise = "roof";
	name = c;
}
	
	public String get_noise()
	{
		return noise;
	}
	public String get_name()
	{
		return name;
	}
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1