public static void printList(ArrayList<Col> list, String place)
{
ArrayList<Col> list2 = list;
int count=0;
float r,g,b;
while(!list2.isEmpty())
{
r = list2.get(count).getR();
g = list2.get(count).getG();
b = list2.get(count).getB();
list2.remove(0);
Log.i(place+": "+count + "", "R: "+r+" G: "+g+" B: "+b);
}
}
this method is removing all the items from my original list for some reason..
i'm thinking maybe the way i duplicate the list is wrong, but i couldnt find the right way.
Since ArrayList is an object:
ArrayList<Col> list2 = list;
is a soft copy. It only copies a reference to the same ArrayList object.
In Java you can clone an object like so:
ArrayList<Col> list2 = (ArrayList<Col>)list.clone();
USE
ArrayList list2 = new ArrayList(list);
INSTEAD OF
ArrayList list2 = list;
Use the following code before assigning the value:
ArrayList<Col> list2 = new ArrayList<Col> ();
Related
I have a list having number of questions in it.
I want to display the total no. of question present in the list.
Loop is used to get particular element at particular index like :
List<?> list = new ArrayList<>();
for(Object obj : list){
//---- do something with obj
}
You do not need loop for size, there are methods to get size of collected data.
Well it not clear which list you are referring to, but here is some example of how you can get size/length of collected data..
if you are referring to ListView,use getCount() :
// --------------ListView
ListView listView = new ListView(context);
int listViewSize = listView.getCount();
And if your referring to any Collection , use size() :
// --------------Collection
ArrayList<?> arrayList = new ArrayList<>();
int arrayListSize = arrayList.size();
List<?> list = new ArrayList<>();
int listSize = list.size();
Set<?> stringSet = new HashSet<>();
int setSize = stringSet.size();
Map<?,?> hashMap = new HashMap<>();
int mapSize = hashMap.size();
LinkedList<?> link = new LinkedList<>();
int linkedListSize = link.size();
Queue<?> queue = new LinkedList<>();
int queueSize = queue.size();
<?> indicate generic, use data-type (<String>,<Integer> etc) for particular return type..
Any other data type array, use length :
// ---------------Data Type
String[] stringsArray = new String[]{};
int stringArraySize = stringsArray.length;
int[] in = new int[]{};
int intSize = in.length;
This is java document for explanation
I believe you are looking for the below. Although the question is not clear.
for(int i = 0;i<list.size();i++){
System.out.println(list.get(i));
}
use size() method to get the size of the list
Suppose we have an ArrayListArrayList(String)
[[a,b,c,d] , [e,f,g,h]]
and an ArrayList(String)
[1,2,3]
How can we add ArrayList to ArrayListArrayList in position 1 at the end in order to get
[[a,b,c,d] , [e,f,g,h,1,2,3]]
Thanks
//pseudocode
List<ArrayList<String>> arraylistarraylist = [[a, b, c, d], [e, f, g, h]];
List<String> list = Arrays.asList(1,2,3);
arraylistarraylist.get(1).addAll(list);
If we name your ArrayList<ArrayList> m and if we name your ArrayList a, then you could use
m.get(1).addAll(a)
Description for this method provided by Oracle here: http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
I think you need a add(int, E) method
List<ArrayList<String>> dataHolder = new ArrayList<ArrayList<String>>();
List<String> firstData = new ArrayList<String>();
firstData.add("a");
firstData.add("b");
firstData.add("c");
firstData.add("d");
List<String> secondData = new ArrayList<String>();
secondData.add("e");
secondData.add("f");
secondData.add("g");
secondData.add("h");
secondData.add("1");
secondData.add("2");
secondData.add("3");
dataHolder.add(secondData);
dataHolder.add(0, firstData); // insert your List<String> to 0 index position in dataHolder
I have:
a String array with an unknown length that's populated with unknown items (let's say fish, bird, cat)
an ArrayAdapter and a Spinner that displays the items
a variable that contains one unknown item from the string array (let's say cat)
I want to set the Spinner to the value from the variable (cat). What's the most elegant solution? I thought about running the string through a loop and comparing the items with the variable (until I hit cat in this example), then use that iteration's # to set the selection of the Spinner, but that seems very convoluted.
Or should I just ditch the Spinner? I looked around and found a solution that uses a button and dialog field: https://stackoverflow.com/a/5790662/1928813
//EDIT: My current code. I want to use "cow" without having to go through the loop, if possible!
final Spinner bSpinner = (Spinner) findViewById(R.id.spinner1);
String[] animals = new String[] { "cat", "bird", "cow", "dog" };
String animal = "cow";
int spinnerpos;
final ArrayAdapter<String> animaladapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, animals);
animaladapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
bSpinner.setAdapter(animaladapter);
for (Integer j = 0; j < animals.length; j++) {
if (animals[j].equals(animal)) {
spinnerpos = j;
bSpinner.setSelection(spinnerpos);
} else {
};
}
(Temporarily) convert your String array to a List so you can use indexOf.
int position = Arrays.asList(array).indexOf(randomVariable);
spinner.setSelection(position);
EDIT:
I understand your problem now. If your String array contains all unique values, you can put them in a HashMap for O(1) retrieval:
HashMap<String, Integer> map = new HashMap<String, Integer>();
for (int i = 0; i < animals.length; i++) {
map.put(animals[i], i);
}
String randomAnimal = "cow";
Integer position = map.get(randomAnimal);
if (position != null) bSpinner.setSelection(position);
I would like to know how to remove duplicate values in Integer Array.
I think you may face This question in all languages.
But in android I don't know how to achieve this.Can anyone please help me to fix this issue.
Thanks in advance...
Be sure your array is Integer type not int
Integer[] array; // Your integer array...
Set<Integer> set = new HashSet<Integer>();
Collections.addAll(set, array);
val finalArrayList: ArrayList<String>
val arrayList = arrayListOf<String>()
arrayList.add("ABC")
arrayList.add("XYZ")
arrayList.add("ABZ")
arrayList.add("ABC")
arrayList.add("XYZ")
arrayList.add("ABZ")
finalArrayList = arrayList.toSet().toList() as ArrayList<String>
Answer
arrayList: [ABC, XYZ, ABZ, ABC, XYZ, ABZ]
finalArrayList: [ABC, XYZ, ABZ]
Try this Code
public static void removeDuplicateWithOrder(ArrayList arlList)
{
Set set = new HashSet();
List newList = new ArrayList();
for (Iterator iter = arlList.iterator(); iter.hasNext();) {
Object element = iter.next();
if (set.add(element))
newList.add(element);
}
arlList.clear();
arlList.addAll(newList);
}
How can I create a list Array (the list display First Alphabet when scroll) with the cursor data?
Go through every element in the Cursor, and add them one by one to the ArrayList.
ArrayList<WhateverTypeYouWant> mArrayList = new ArrayList<WhateverTypeYouWant>();
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
// The Cursor is now set to the right position
mArrayList.add(mCursor.getWhateverTypeYouWant(WHATEVER_COLUMN_INDEX_YOU_WANT));
}
(replace WhateverTypeYouWant with whatever type you want to make a ArrayList of, and WHATEVER_COLUMN_INDEX_YOU_WANT with the column index of the value you want to get from the cursor.)
One quick correction: the for loop above skips the first element of the cursor.
To include the first element, use this:
ArrayList<String> mArrayList = new ArrayList<String>();
mCursor.moveToFirst();
while(!mCursor.isAfterLast()) {
mArrayList.add(mCursor.getString(mCursor.getColumnIndex(dbAdapter.KEY_NAME))); //add the item
mCursor.moveToNext();
}
Even better than #imbrizi's answer is this:
ArrayList<String> mArrayList = new ArrayList<String>();
while(mCursor.moveToNext()) {
mArrayList.add(mCursor.getString(mCursor.getColumnIndex(dbAdapter.KEY_NAME))); //add the item
}
moveToNext() will return false if there isn't anything left, so this reduces the SLOC by a few, and is easier to see.
Even better is to get the column index outside of the loop.
ArrayList<String> mArrayList = new ArrayList<String>();
int columnIndex=mCursor.getColumnIndex(dbAdapter.KEY_NAME)
while(mCursor.moveToNext()) {
mArrayList.add(mCursor.getString(columnIndex)); //add the item
}
This one worked really well for me because I wanted an arraylist of objects:
List<MyObject> myList = new ArrayList<String>();
Cursor c = ...
while(c.moveToNext()) {
myList.add(new MyObject(cursor.getInt(cursor.getColumnIndex("_id")), cursor.getString(cursor.getColumnIndex("column1")), cursor.getInt(cursor.getColumnIndex("column2")));
}
c.close();
Just make a POJO MyObject and make sure it has a constructor.
In Kotlin you can use this extension:
fun <T> Cursor.toList(block: (Cursor) -> T) : List<T> {
return mutableListOf<T>().also { list ->
if (moveToFirst()) {
do {
list.add(block.invoke(this))
} while (moveToNext())
}
}
}
and use it:
val listOfIds = cursor.toList {
// create item from cursor. For example get id:
it.getLong(it.getColumnIndex("_id"))
}