如何复制java array-list中的元素?

时间:2014-12-09 01:38:02

标签: java android xml arraylist

我有一个不同运动的阵列表。我创建了我的for循环来搜索数组列表,然后我必须将我的元素从gfList复制到gfResult我将要传递给活动。最后是我的数组列表。我不知道我必须在switch语句中传递我的结果,或者在每个活动的private void部分中传递我的结果以及假设是什么。提前致谢!这是我的代码,但我的代码不起作用:

GroupedFeed findFeed(String locateSport){ 
                if ((!gfList.isEmpty())){
                    public static void main(String args[]){
                    int [] gfList= new int[ gfResult.length ] ;
                    for (int i = 0; i != gfList.size();i++){
                        if (gfList.get(i).category.equalsIgnoreCase(locateSport)){
                           gfResult = Array.copyOf(gfList, gfList.length);
                        }
                        return gfResult;
                    }
                 }
                return null;
               }
        }


        public boolean onChildClick(ExpandableListView parent,
                    android.view.View v, int groupPosition, int childPosition,
                    long id) {
                switch (groupPosition)
                {
                case 0:
                        switch (childPosition)
                        {
                        case 0:
                            // gfResult = findFeed("Men's Baseball");
                            if(gfResult != null){
                                Baseball();
                            }
                              // pass gfResult
                            break;
                          }
                return false;
            }

            private void Baseball() {
                Intent myIntent = new Intent(MainActivity.this, Baseball.class);
                startActivity(myIntent);
            }

Array-list xml文件:

 this   Main  (id=831962574768) 
groupedFeeds    ArrayList  (id=831963150464)    
    array   Object[12]  (id=831963153088)   
        [0] GroupedFeed  (id=831963152968)  
            category    "Women's Golf" (id=831962986192)    
            feeds   ArrayList  (id=831963152992)    
        [1] GroupedFeed  (id=831963153592)  
            category    "Volleyball" (id=831962991720)  
            feeds   ArrayList  (id=831963153616)    
        [2] GroupedFeed  (id=831963153744)  
            category    "Men's Soccer" (id=831962996544)    
            feeds   ArrayList  (id=831963153768)    
        [3] GroupedFeed  (id=831963153896)  
            category    "Women's Soccer" (id=831963006320)  
            feeds   ArrayList  (id=831963153920)    
        [4] GroupedFeed  (id=831963154864)  
            category    "Men's Golf" (id=831963016488)  
            feeds   ArrayList  (id=831963154888)    
        [5] GroupedFeed  (id=831963155072)  
            category    "Men's Cross Country" (id=831963036816) 
            feeds   ArrayList  (id=831963155096)    
        [6] GroupedFeed  (id=831963155224)  
            category    "Women's Cross Country" (id=831963041984)   
            feeds   ArrayList  (id=831963155248)    
        [7] GroupedFeed  (id=831963155472)  
            category    "Men's Bowling" (id=831963093056)   
            feeds   ArrayList  (id=831963155496)    
        [8] GroupedFeed  (id=831963155712)  
            category    "Women's Bowling" (id=831963098224) 
            feeds   ArrayList  (id=831963155736)    
        [9] GroupedFeed  (id=831963155864)  
            category    "Women's Basketball" (id=831963170720)  
            feeds   ArrayList  (id=831963155888)    
        [10]    GroupedFeed  (id=831963157504)  
            category    "Men's Basketball" (id=831963299944)    
            feeds   ArrayList  (id=831963157528)    
        [11]    null    
    modCount    11  
    size    11  
loader  RSSLoader  (id=831962575480)    
aMan    AssetManager  (id=831962469744) 

1 个答案:

答案 0 :(得分:0)

我建议您使用Collection类及其子类,例如ArrayList。它们的大小是动态分配的,您不必每次都分配足够的数组大小。 E.g:

Vector<Integer> list=new Vector<Integer>();

向其添加元素

list.add(123);
list.add(133);

导出到数组

Integer out[]=list.toArray(new Integer[list.size()]);