Retrieving an element from array list in Android?

后端 未结 6 2044
醉话见心
醉话见心 2021-02-01 07:16

I am trying to implement voice recognition code in Android. How do I get an element at a particular position from array list in Android? I tried converting arraylist

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 07:54

    public class DemoActivity extends Activity {
        /** Called when the activity is first created. */
        ArrayList al = new ArrayList();
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
    
    
            // add elements to the array list
    
            al.add("C");
            al.add("A");
            al.add("E");
            al.add("B");
            al.add("D");
            al.add("F");
    
            // retrieve elements from array
    
            String data = al.get(pass the index here);
            System.out.println("Data is "+ data);
    

    This is another way of getting element

            Iterator it = al.iterator();
            while (it.hasNext()) {
                System.out.println("Data is "+ it.next());
            }
        }
    

提交回复
热议问题