Remove() object from an ArrayList

edited December 2013 in How To...

I've read a few remove() posts on this forum, and while my problem is much more simple, I still can't figure it out. I understand that remove() removes an int, but I think I've tried that. Am I on the right track with:

remove(foundLink); // doesnt work because not an int

or

foundLink.remove(a); // doesnt work, not sure why...

Any help on this matter would be greatly appreciated. A portion of my code below. Thanks!

void searchEndLinks () {

   htmlList = new HtmlList(URL_answer); 
   ArrayList makeLinks= (ArrayList) htmlList.getLinks(); 
    for(int a = 10;a<50;a++){ 

              String temp = makeLinks.get(a).toString();
              String [] wikiLinkTemp = split(temp, "url:"); 
              String []wikiLink = trim(wikiLinkTemp);   
              String foundLink = baseURL + wikiLink[1];
            //println("This is wikilink FOUNDLINK " + foundLink);


           if( foundLink.contains(valueToRemove) == true) { 
            println("found a match");
               remove(foundLink);
               //foundLink.remove(a);
             }

              // println("this is the size of FoundLINK forFINAL before remove = " + foundLink.length());
              finalNodes.add(foundLink); 
              } 

}

Answers

  • _vk_vk
    edited December 2013 Answer ✓

    Check this out . I didn't run your code (can't).

    I understand that remove() removes an int

    No (you cant even put an int in an arrayList, only objects, unless wrapped in an Integer...) remove takes an int as argument, this int is the index of the element to be removed. (There is another flavour of remove which takes an object, let's skip this for now :).

    look this sample:

    ArrayList<Integer> ints = new ArrayList<Integer>();
    int index = 10;
    void setup() {
      for (int i = 0; i < 10; i++) {
        ints.add(new Integer (i));
      }
    
      println("first print");
      println(ints + "\n");
    }
    void draw() {
    }
    
    void keyPressed() {
    
      if (index > 0) {
        index --;
        ints.remove(index);
      }
      println(ints + "\n");
    }
    
  • I reformatted your code (with Ctrl+T in the PDE) and added some comments on your coding...

    void searchEndLinks() {
    
      htmlList = new HtmlList(URL_answer);
      ArrayList makeLinks = (ArrayList) htmlList.getLinks();
      for (int a = 10; a < makeLinks.size(); a++) { // Don't use 50! Hard-coded numbers are generally bad...
      // Or use min(50, makeLinks.size()) if you want to limit the number
    
        String temp = makeLinks.get(a).toString();
        String[] wikiLinkTemp = split(temp, "url:");
        String[] wikiLink = trim(wikiLinkTemp);  
        String foundLink = baseURL + wikiLink[1];
        //println("This is wikilink FOUNDLINK " + foundLink);
    
    
        if (foundLink.contains(valueToRemove)) { // No need for == true
          println("found a match");
          makeLinks.remove(foundLink); // That's the main point! You have to tell which array list you remove from...
        }
    
        // println("this is the size of FoundLINK forFINAL before remove = " + foundLink.length());
        finalNodes.add(foundLink);
      }
    }
    

    You have to tell which array list you want to remove from: you can have several of them in your sketch!

Sign In or Register to comment.