Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: how to remove items of an arraylist

  1. #1
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to remove items of an arraylist

    hey guys,
    i got a little question about my code...
    does the code removes the int of the arraylist? and gives it a true value out if it is removed?
    	public boolean kL(int ktonummer){
    		if (finKntoIdex(ktonummer) > 0) { 
    			konten.remove(ktonummer);
    			return true;
    		}
    		else 
    		return false;
    	}
    	private int finKntoIdex(int ktonummer){
    		for(int i = 0;i<konten.size();i++){
    			if(konten.get(i).getKontonummer() == ktonummer){
    				return i;
    			}
    		}
    		return -1;
    	}
     
    private ArrayList<Konto> konten = new ArrayList<Konto>();
    Last edited by Norm; December 16th, 2012 at 01:58 PM. Reason: changed quote to code tags


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to remove items of an arraylist

    Have you tried testing the kL() method to see what happens when it is executed?
    Create a small test program with an arraylist, call the kL() method to have it remove an element and then print out the arraylist to see if the element has been removed.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to remove items of an arraylist

    yes thanks mate it worked !

    a other question my toString method will not work...
    @Override
    public String toString(){
    return String.format("Kontonummer: %s, Nachname: %s" , kten.kontonummer + konten.nachname);


    }

    private ArrayList<Konto> kten = new ArrayList<Konto>();

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to remove items of an arraylist

    toString method will not work...
    Please explain.
    Post full text of any error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to remove items of an arraylist

    kten.kontonummer and kten.nachname cannot be resolved or is not a field ...
    but i have declared it in a other class

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to remove items of an arraylist

    Are you saying the compiler can not find those variables?
    Some possible reasons:
    Are they defined?
    Did you spell the names correctly?
    Are the definitions in scope where you are trying to use them?

    Without seeing the source, I have no idea why.


    BTW what you posted is not the full text of the compiler's error messages.
    Here is a sample:
    TestSorts.java:138: cannot find symbol
    symbol  : variable var
    location: class TestSorts
             var = 2;
             ^
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to remove items of an arraylist

    yes they are in the right scope but not on the same class
    the first code is at the class bank.
    @Override
    public String toString(){
    return String.format("Kontonummer: %s, Nachname: %s" , kten.kontonummer + kten.nachname);


    }

    private ArrayList<Konto> kten = new ArrayList<Konto>();
    there i work with the arraylist for example
    public boolean kontoLoeschen(int ktonummer){

    if (findeKontoIndex(ktonummer) > 0) {
    kten.remove(ktonummer);
    return true;
    }
    else
    return false;
    or

    private int findeKontoIndex(int ktonummer){
    for(int i = 0;i<konten.size();i++){
    if(kten.get(i).getKtonummer() == ktonummer){
    return i;
    }
    }
    return -1;
    }

    }
    but i have declared it in a other class called kten or prson
    public String getNachname() {
    return nachname;
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: how to remove items of an arraylist

    Please post the full text of the compiler's error messages.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Need some help with removing and finding items from an ArrayList
    By bankston13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 30th, 2012, 08:51 PM
  2. Adding add/remove button to add/remove tab
    By JMtrasfiero in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 27th, 2012, 11:24 AM
  3. [SOLVED] Looping through ArrayList to remove elements
    By itispj in forum Collections and Generics
    Replies: 3
    Last Post: October 14th, 2011, 12:42 AM
  4. How do I display 4 items at a time of an ArrayList?
    By JavaStruggler in forum Member Introductions
    Replies: 5
    Last Post: August 9th, 2011, 02:33 PM
  5. not able to remove from ArrayList
    By harsha_c in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 03:28 AM