Results 1 to 10 of 10
  1. #1
    Xeal Rebad is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default LinkedList Problems (trimming or something)

    Hey all, i am pleased to say that I think i have a better understanding of how java works now but i have run into a problem when using linked list. I want to make a list full of objects and when i make a call directly to the list eg list.add(object) everything is fine and i can add as many objects as i want, however when i create a method to do this (for encapsulation purposes) it only lets me add three objects to the list then stops adding more even if i call the method again.:confused:

    If anyone knows whats happening i would really appreciate some advice thanks

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Not without seeing your code. If it is too long create a SSCCE (google it).

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Quote Originally Posted by Xeal Rebad View Post
    If anyone knows whats happening...
    No one would know whats happening unless you show them what have you done actually... We can't guess it, right?

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by goldest View Post
    We can't guess it, right?
    We can actually. I guess it is because OP is wearing green socks.

  5. #5
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Quote Originally Posted by Junky View Post
    I guess it is because OP is wearing green socks.
    Right... LOL ;)
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

  7. #7
    Xeal Rebad is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    public class runThis {
    	Cat cat1 = new Cat();
            Cat cat2 = new Cat();
            Dog dog1  = new Dog();
            Dog dog1  = new Dog();
    	public static void main(String[] args){
    		AnimalList animalList = new AnimalList();
    		animalList.List.add(Cat1);
    		animalList.List.add(Cat2);
    		animalList.List.add(Dog1);
    		animalList.List.add(Dog2);
    		System.out.println("Start");
    		System.out.println(animalList.List.size());
    		for(Animal creature:animalList.List){
    			System.out.println(creature);
    		}   
         
                    AnimalList animalList = new AnimalList();
    		animalList.addAnimal(Cat1);
    		animalList.addAnimal(Cat2);
    		animalList.addAnimal(Dog1);
    		animalList.addAnimal(Dog2);
    		System.out.println("Start");
    		System.out.println(animalList.List.size());
    		for(Animal creature:animalList.List){
    			System.out.println(creature);
    		}   
    	}
    }
    Java Code:
    public class animalList{
                    public ArrayList<Animal> List = new ArrayList<Animal>();
    		
                    public void addAnimal(Animal theAnimal){
    			List.add(theAnimal);
                    }
    }
    So when i insert the animals in the first way, i can insert as many as i want, but when i insert them using my addAnimal method it only lets me add 3. Any ideas

  8. #8
    Xeal Rebad is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default

    oh and green socks? lol

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    I asked for a SSCCE which stands for Short Self-Contained Compilable Example. Your code is far from compilable.

    What makes you think that you can only add 3 objects to the list? If you correct the multitude of errors, the code will indeed work for 4 or more objects.

  10. #10
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    6

    Cool

    Exactly as Junky said... The code will will work if OP can understand that Java is case sensitive. Which means that Cat1 is NOT the same as cat1.

    Plus, duplicate variables [like dog1] are not allowed unless you have customized your compiler to work as per your own wish.

    Goldest
    Last edited by goldest; 05-23-2011 at 04:32 PM.
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

Similar Threads

  1. Help with trimming a Double value
    By ozzyman in forum New To Java
    Replies: 4
    Last Post: 03-13-2011, 03:37 PM
  2. Trimming trailing zeros in a byte
    By Aaron_Sharp in forum New To Java
    Replies: 1
    Last Post: 01-06-2011, 12:04 PM
  3. Trimming of array
    By Paulious1 in forum New To Java
    Replies: 0
    Last Post: 09-01-2010, 03:18 PM
  4. Trimming a String
    By PhilArmstrong in forum New To Java
    Replies: 3
    Last Post: 11-10-2009, 09:01 PM
  5. Trimming URL
    By Juuno in forum New To Java
    Replies: 1
    Last Post: 03-10-2009, 06:17 PM

Tags for this Thread

Posting Permissions

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