how does arraylist add work code example
Example 1: java arraylist add
ArrayList<String> arrayList = new ArrayList<String>();
arrayList.add("item");
System.out.println(arrayList.contains("item"));
arrayList.remove("item");
Example 2: Java arraylist if you don't want to use add()
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView myListView = (ListView) findViewById(R.id.myListView);
final ArrayList<String> myFriendsList = new ArrayList<String>(asList("Friend1", "Friend2", "Friend3", "Friend4", "Friend5"));
ArrayAdapter<String> friendsArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, myFriendsList);
myListView.setAdapter(friendsArrayAdapter);
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, myFriendsList.get(position), Toast.LENGTH_SHORT).show();
}
});
}
}