473,324 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.
Feb 14 '09 #1
3 7268
umbr
9
Hi, chanshaw.
Just a simple sample :)
Expand|Select|Wrap|Line Numbers
  1. public static String findInList(List<Object> list, Object pattern)
  2. {
  3.     for(Object o : list)
  4.         if(o.equals(pattent)
  5.             return o.toString();
  6.  
  7.     return null;
  8. }
  9.  
Instead Object type you can use another one.
Feb 14 '09 #2
Ok that didn't really help but thanks here's my code

Expand|Select|Wrap|Line Numbers
  1. import java.sql.Statement;
  2. import java.io.BufferedReader;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.util.ArrayList;
  9.  
  10. /**
  11.  * @date February 14, 2009
  12.  * @author Colin Hanshaw
  13.  */
  14.  
  15. public class ProcessChecker
  16. {
  17.     String parsedString;
  18.     String test;
  19.     int counter = 0;
  20.     ArrayList parsedStrings = new ArrayList();
  21.     ArrayList blockedApps = new ArrayList();
  22.     private static String dbURL = "jdbc:derby://localhost:1527/MyDesk;user=administrator;password=password";
  23.     Connection conn = null;
  24.     boolean running = true;
  25.  
  26.     public static void main(String[] args) throws Exception
  27.     {
  28.         ProcessChecker checker = new ProcessChecker();
  29.     }
  30.  
  31.     public ProcessChecker() throws Exception
  32.     {
  33.         createConnection();
  34.         listRunningProcesses();
  35.         quereyDatabase();
  36.         tryFind();
  37.     }
  38.  
  39.     private void createConnection()
  40.     {
  41.         try
  42.         {
  43.             Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  44.             conn = DriverManager.getConnection(dbURL);
  45.         }
  46.         catch (Exception except)
  47.         {
  48.             except.printStackTrace();
  49.         }
  50.     }
  51.  
  52.     public void listRunningProcesses()
  53.     {
  54.         try
  55.         {
  56.             Runtime runtime = Runtime.getRuntime();
  57.             String cmds[] = {"cmd", "/c", "tasklist"};
  58.             Process proc = runtime.exec(cmds);
  59.             InputStream inputstream = proc.getInputStream();
  60.             InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
  61.             BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
  62.             String line;
  63.             while ((line = bufferedreader.readLine()) != null)
  64.             {
  65.                parsedString = parseString(line);
  66.                parsedStrings.add(new String(parsedString));
  67.                counter++;
  68.             }
  69.             removeElements();
  70.         }
  71.         catch(Exception ex)
  72.         {
  73.             System.out.println("Tasklist might not exist");
  74.         }
  75.     }
  76.  
  77.     public String parseString(String incString)
  78.     {
  79.         String myParsedString = incString;
  80.         String delims = "[ ]+";
  81.         String[] tokens = incString.split(delims);
  82.         myParsedString = tokens[0];
  83.         return myParsedString;
  84.     }
  85.  
  86.     public void removeElements()
  87.     {
  88.         parsedStrings.remove(0);
  89.         parsedStrings.remove(0);
  90.         parsedStrings.remove(0);
  91.     }
  92.  
  93.     public void quereyDatabase() throws Exception
  94.     {
  95.        try
  96.        {
  97.            Statement stmt = conn.createStatement();
  98.            ResultSet rs;
  99.            rs = stmt.executeQuery("select BLOCKEDITEMS from ADMINISTRATOR.BLOCKEDLIST");
  100.            while ( rs.next() )
  101.            {
  102.                String blockedItem = rs.getString("BLOCKEDITEMS");
  103.                blockedApps.add(new String(blockedItem));
  104.            }
  105.            conn.close();
  106.        }
  107.  
  108.        catch (Exception e)
  109.        {
  110.            System.err.println("Got an exception! ");
  111.            System.err.println(e.getMessage());
  112.        }
  113.     }
  114.  
  115.     public void tryFind()
  116.     {
  117.  
  118.     }
  119.  
  120.  
  121.  
  122.     /*public void endProcess()
  123.     {
  124.         if(line.startsWith(testString))
  125.                 {
  126.                     runtime.exec("taskkill /IM " + testString);
  127.                 }
  128.     }*/
  129. }
  130.  
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.
Feb 15 '09 #3
JosAH
11,448 Expert 8TB
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
Feb 15 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Robert Linder | last post by:
I am trying to compare arraylists in csharp. I setup a simple test with empty arraylists. string x1 = {}; string x2 = {}; Console.WriteLine( x1 == x2 ); // false...
5
by: Joe Black | last post by:
Hi all, Using Windows CP Pro VS .net 2005 I'm creating an app that allows user to extend its functionality by installing plug in modules, these modules support an interface I have created...
2
by: David | last post by:
Hi all, How would I go about this? I am doing a document management system and I need to write a clean up routine. I will create two arrays. One of them will have a list of filenames I have...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
0
by: richardkreidl | last post by:
I have the following hash script that I use to compare two text files. 'Class Public Class FileComparison Public Class FileComparisonException Public Enum ExceptionType U 'Unknown A 'Add...
5
by: paolo.ponzano | last post by:
Hello, I've to implement a IComparer in order to sort an arraylist. I've used reflection to get the object I need to sort, at the moment I'm using the following piece of code: public int...
3
by: steve | last post by:
I need to compare the value of a field in a row on an arraylist with the value of a field on a second arraylist I have this bit of code working for arrays but cant get it working for arraylists The...
10
by: netnet | last post by:
I have a collection of objects that I store in the session. Then I look in that collection to see if the collection.contains an object. it answers false. If I dont store that collection in the...
0
by: ccshine via DotNetMonster.com | last post by:
I'm working on an app that implements a Structure to store a recordset in an ArrayList. I used this setup to bind to a DataGrid and it worked out so well, I thought it might be a better solution...
3
by: =?Utf-8?B?Sm9zaFA=?= | last post by:
Hi All, I am attempting to compare values in two arraylists to make sure all the values are the same. I am running into trouble with my code if both arraylists compare okay up until a point and I...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.