English 中文(简体)
在ArrayList <string>
原标题:Check if String in String[] is in ArrayList<string>

我刚刚建造了广播接收器,我可以拿到新文本的电文,而我是在有空间时将案文信息分开,将其保留到“条码”。

现在,我需要检查一下,在<代码>中,标准是我数据库中的内容。 为此,我设立了一个<代码>ArrayList<String>,从相应的栏目中获取所有条目。 现在,我需要检查一下,在我的<密码>中,“射线/代码”中的“强硬”是否与文本电文相同,但我不知道如何认识到这一点。

是否有容易和快速的办法来检查,我还需要知道这两者中有哪些优势?

<>SmileySmsReceiver:

  package de.retowaelchli.filterit.services;


    import java.util.ArrayList;

    import de.retowaelchli.filterit.database.SFilterDBAdapter;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    import android.util.Log;
    import android.widget.Toast;



    public class SmileySmsReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) 
        {
            //Datenbank definieren
            SFilterDBAdapter mDbHelper = new SFilterDBAdapter(context);


            //---get the SMS message passed in---
            Log.d("SmileySmsReceiver", "Yes it calls the onReceive");
            Bundle bundle = intent.getExtras();        
            SmsMessage[] msgs = null;
            String str = "";            
            if (bundle != null)
            {
                Log.d("SmileySmsReceiver", "Bundle is not null");
                //---retrieve the SMS message received---
                Object[] pdus = (Object[]) bundle.get("pdus");
                msgs = new SmsMessage[pdus.length];            
                for (int i=0; i<msgs.length; i++){
                    msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
                    str += "SMS from " + msgs[i].getOriginatingAddress();                     
                    str += " :";
                    str += msgs[i].getMessageBody().toString();
                    str += "
";        
                Log.d("SmileySmsReceiver","Was steht in der Nachricht?: " + str);

                String[] splited = str.split("\s+");

                //Hier werden die Strings der Smileys aus der Datenbank gezogen
                mDbHelper.open();
                Cursor c = mDbHelper.getAllSFilter();

                ArrayList<String> SmileyList = new ArrayList<String>();
                c.getColumnIndex(SFilterDBAdapter.KEYWORD);
                int ColumnIndex = c.getColumnIndex(SFilterDBAdapter.KEYWORD);
                    if(c!=null)
                        {
                        //Hier werden die Smileys in die ArrayList geschrieben
                        while(c.moveToNext()){
                            String infoItem = c.getString( ColumnIndex );
                            SmileyList.add(infoItem);
                        }
<------------------------- FROM HERE ON I NEED YOUR GUYS HELP ------------------------------->
                }
                }
                //---display the new SMS message---
                Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
            }                         
        }
    }
最佳回答

如果有<代码>。 查阅我的标准ArrayList<String> myListOfStrings时,请在名单上填写:

myListOfStrings.contains( myString );

This will return a boolean (true/false) - true if the strings was found, false if not.

In your case you would want to run through the array of strings and match each of them to the SmileyList like this:

for( String s : splitted ) {
   if( SmileyList.contains( s ) ) {
     //Do what you need to do if the string is in the SmileyList.
   }
}
问题回答
String array[] = ... ; 
List<String> list = ... ;
// check if anything in array is in list
for(String str : array)
    if(list.contains(str)) doSomething() ;

如果你们想做的是每一件事,上述法典将发挥作用。 如果你只想做一番事情(而不是对每一件都一样),那么,如果句话的话,你就必须在句子中添加一个<条码><>>>。

You can iterate over the array (e.g. with a for loop over splitted.length), querying your list with myList.contains(splitted[i]). The query will return a boolean.

如果需要核对在清单中阵列中的all项,则有一条通融的备选案文:myList.containsAll(Arrays.asList(splitted))

还有一个简明的备选方法,不去检查any。 清单中列有阵列的项目:

List splitList = Arrays.asList(splitted);
splitList.retainAll(myList);

<>代码>splitList.isEmpty() 然后将<代码>false退回,条件是:任何项目在清单中对应。

<代码>splitList的内容将显示所有与之相匹配的示意图。

Depending on how large your array and ArrayList are and how many times you will access them, you might want to use a HashSet instead of an ArrayList so that you can get a fast look up with the hashed contains() method instead of having to iterate through the ArrayList. Something like this:

boolean containsString(ArrayList<String> stringArrayList, String[] stringArray) {
    Set<String> stringSet = new HashSet<String>(stringArrayList); // if same input is used reused, might want this outside the method

    for (String s : stringArray) {
        if (stringSet.contains(s)) {
            return true;
        }
    }

    return false;
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签