Comparing arrayList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • chanshaw
    New Member
    • Nov 2008
    • 67

    Comparing arrayList

    Alright I need a little help here I want to compare to arraylists and if a match occurs return the value of whatever item matched as a string. Thanks alot.
  • umbr
    New Member
    • Feb 2009
    • 9

    #2
    Hi, chanshaw.
    Just a simple sample :)
    Code:
    public static String findInList(List<Object> list, Object pattern)
    {
        for(Object o : list)
            if(o.equals(pattent)
                return o.toString();
    
        return null;
    }
    Instead Object type you can use another one.

    Comment

    • chanshaw
      New Member
      • Nov 2008
      • 67

      #3
      Ok that didn't really help but thanks here's my code

      Code:
      import java.sql.Statement;
      import java.io.BufferedReader;
      import java.io.InputStream;
      import java.io.InputStreamReader;
      import java.sql.Connection;
      import java.sql.DriverManager;
      import java.sql.ResultSet;
      import java.util.ArrayList;
      
      /**
       * @date February 14, 2009
       * @author Colin Hanshaw
       */
      
      public class ProcessChecker
      {
          String parsedString;
          String test;
          int counter = 0;
          ArrayList parsedStrings = new ArrayList();
          ArrayList blockedApps = new ArrayList();
          private static String dbURL = "jdbc:derby://localhost:1527/MyDesk;user=administrator;password=password";
          Connection conn = null;
          boolean running = true;
      
          public static void main(String[] args) throws Exception
          {
              ProcessChecker checker = new ProcessChecker();
          }
      
          public ProcessChecker() throws Exception
          {
              createConnection();
              listRunningProcesses();
              quereyDatabase();
              tryFind();
          }
      
          private void createConnection()
          {
              try
              {
                  Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                  conn = DriverManager.getConnection(dbURL);
              }
              catch (Exception except)
              {
                  except.printStackTrace();
              }
          }
      
          public void listRunningProcesses()
          {
              try
              {
                  Runtime runtime = Runtime.getRuntime();
                  String cmds[] = {"cmd", "/c", "tasklist"};
                  Process proc = runtime.exec(cmds);
                  InputStream inputstream = proc.getInputStream();
                  InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
                  BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
                  String line;
                  while ((line = bufferedreader.readLine()) != null)
                  {
                     parsedString = parseString(line);
                     parsedStrings.add(new String(parsedString));
                     counter++;
                  }
                  removeElements();
              }
              catch(Exception ex)
              {
                  System.out.println("Tasklist might not exist");
              }
          }
      
          public String parseString(String incString)
          {
              String myParsedString = incString;
              String delims = "[ ]+";
              String[] tokens = incString.split(delims);
              myParsedString = tokens[0];
              return myParsedString;
          }
      
          public void removeElements()
          {
              parsedStrings.remove(0);
              parsedStrings.remove(0);
              parsedStrings.remove(0);
          }
      
          public void quereyDatabase() throws Exception
          {
             try
             {
                 Statement stmt = conn.createStatement();
                 ResultSet rs;
                 rs = stmt.executeQuery("select BLOCKEDITEMS from ADMINISTRATOR.BLOCKEDLIST");
                 while ( rs.next() )
                 {
                     String blockedItem = rs.getString("BLOCKEDITEMS");
                     blockedApps.add(new String(blockedItem));
                 }
                 conn.close();
             }
      
             catch (Exception e)
             {
                 System.err.println("Got an exception! ");
                 System.err.println(e.getMessage());
             }
          }
      
          public void tryFind()
          {
              
          }
          
      
          
          /*public void endProcess()
          {
              if(line.startsWith(testString))
                      {
                          runtime.exec("taskkill /IM " + testString);
                      }
          }*/
      }
      So here's the arraylists

      parsedStrings contains

      String "Chris"
      String "Nick"
      String "Tom"
      String "Melissa"
      String "Wombat"

      blockedApps contains

      String "Tyler"
      String "Chris"
      String "Art"
      String "Melissa"

      I want the tryFind() method to compare the two arraylists

      So it would return true for Chris
      Then would return true for Melissa

      Thanks hope that made more sence.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Read the API docs for the List interface; it has a boolean contains() method; call that method for every element in the other list; or, if you want to go fancy, have a look at the retainAll() method.

        kind regards,

        Jos

        Comment

        Working...