2013-05-07 97 views
1

我是新來的機器人..一個arraylist到另一個arraylist?

我正在應對一個字符串arraylist值到另一個字符串arraylist,我想複製出重複。請任何一個可以幫助我...

非常感謝..

public class Videoplayer extends Activity { 
private GridView girGridView; 
public static String hs = ""; 
public static ArrayList<String> Array = new ArrayList<String>(); 
public static ArrayList<String> resim_list=new ArrayList<String>(); 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_videoplayer); 
    Bundle bundle = getIntent().getExtras(); 
    ArrayList<String> Array = bundle.getStringArrayList("string-array"); 
    GridViewConfig.addImageUrls(); 
    System.out.println("String Video"+ Array); 
    girGridView=(GridView) findViewById(R.id.gridView1_bir); 
    girGridView.setAdapter(new ImageAdapter(this)); 
    girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
        @Override 
        public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) { 
        Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();        
        } 
      }); 
} 
    public static class GridViewConfig { 
    public static ArrayList<String> resim_list=new ArrayList<String>(); 
    public static ArrayList<String> getResim_list() { 
      return resim_list; 
    } 
    public void setResim_list(ArrayList<String> resim_list) { 
      GridViewConfig.resim_list = resim_list; 
    } 
    public static void addImageUrls() { 
      Set<String> hs = new LinkedHashSet<String>(Array);// set of unique elments 
     // LinkedHashSet maintains insertion order 
      resim_list.addAll(hs); 
    System.out.println("NEW RESIM_LIST" + resim_list); 

這怎麼辦???

+0

檢查下面的答案 – Raghunandan 2013-05-07 10:28:29

回答

0
public static ArrayList<String> Array = new ArrayList<String>(); 

    public static ArrayList<String> resim_list=new ArrayList<String>(); 

在你的活動

Array = bundle.getStringArrayList("string-array"); 
    // already Array is initalized. no need to initialize again 
    GridViewConfig.addImageUrls();// call the add method 

GridViewConfig類

public static class GridViewConfig { 
    public static ArrayList<String> resim_list=new ArrayList<String>(); 

    public static ArrayList<String> getResim_list() { 
     return resim_list; 
    } 

    public void setResim_list(ArrayList<String> resim_list) { 
     GridViewConfig.resim_list = resim_list; 
    } 

    public static void addImageUrls() { 
     Set<String> hs = new LinkedHashSet<String>(Array);// set of unique elments 
     // LinkedHashSet maintains insertion order 
     resim_list.addAll(hs); 
     for(int i=0;i<resim_list.size();i++) 
     { 
      System.out.println("NEW RESIM_LIST" + resim_list.get(i)); 

     } 
    } 
} 

編輯:

聲明此之前的onCreate()

public ArrayList<String> Array = new ArrayList<String>(); 
public ArrayList<String> resim_list=new ArrayList<String>(); 

在你的onCreate()

Array = bundle.getStringArrayList("string-array"); 
addImageUrls(); 

然後定義在活動類addImageUrls如下

 public void addImageUrls() { 
     ArrayList<String> resim_list=new ArrayList<String>(); 
     Set<String> hs = new LinkedHashSet<String>(Array);// set of unique elments 
     // LinkedHashSet maintains insertion order 
     resim_list.addAll(hs); 
     for(int i=0;i<resim_list.size();i++) 
     { 
      System.out.println("NEW RESIM_LIST" + resim_list.get(i)); 
     } 
     } 

陣列被聲明用類範圍。不需要使用靜態修改。如果你打算在另一個類中使用它,則使用意圖傳遞它。

-1

您可以使用addAll方法。

//without duplicated, use Set 
Set setTemp = new HashSet(Array); 
resim_list.addAll(setTemp); 
+0

採購訂單請求「我想複製沒有重複」 – Frank 2013-05-07 07:24:28

1

使用,集的集合類

Set<String> set = new HashSet<String>(Array); 
resim_list.addAll(set); 

現在這裏Set將從您ArrayList刪除所有重複值。

+0

如果我打印它顯示空這樣resim_list [] – 2013-05-07 07:33:20

+0

如何打印它?給我看一個代碼。 – user370305 2013-05-07 07:33:49

+0

Set set = new HashSet(Array); \t \t resim_list.addAll(set); \t \t System.out.println(「RESIM_LIST」+ resim_list); – 2013-05-07 07:36:17

0

可以使原來的數組列表
類一{

public static ArrayList<String> Array in class One . 
} 

並希望在任何其他的類此數組列表時,根本就

class Two { 
      ArrayList<String> Array2 = One.Array; 
} 
0

要確保你沒有得到任何如果它包含「resim_list」中的元素,則可以遍歷「resim_list」並檢查「Array」

for(String item: resim_list){ 
    if(Array.contains(item)) 
    { 
     Array.remove(item); 
    }  
} 

然後你可以在其他兩個答案中使用代碼。

Set set = new HashSet(Array); 
resim_list.addAll(set); 

而且你不應該有任何重複。

相關問題