1

i'm working in Android Application, i'm looking how to display results from array list after for loop, i want to set every value (heure, minute ...) in another list view after the function of conversion (BCD to Integer) that i'm worked with. thank you for your help!!

Integer[] array = {heure, minute, seconde, jour, mois, year};
    ArrayList<Integer> list = new ArrayList<>();
    /*Collections.addAll(list, array);*/
    for (int value : list) {
        temp = value;
        temp >>= 4;
        result = temp * 10;
        temp = value & 0b00001111;
        result += temp;
        this.total= result;
        list.add(this.total);
    }
    tvGetdata.setText(list.toString());
2
  • 1
    Can you be more specific what your actual question is or where you are stuck? Commented Feb 28, 2017 at 14:11
  • i want to display every value converted in another arraylist Commented Feb 28, 2017 at 14:16

2 Answers 2

1

You're using a foreach on a empty list. So your code will just ignore it. Try do your for using your array.

Integer[] array = {heure, minute, seconde, jour, mois, year};
ArrayList<Integer> list = new ArrayList<>();
for (int value : array) {
    temp = value;
    temp >>= 4;
    result = temp * 10;
    temp = value & 0b00001111;
    result += temp;
    this.total= result;
    list.add(this.total);
}
tvGetdata.setText(list.toString());
Sign up to request clarification or add additional context in comments.

Comments

0

You kind of misused the two data structures here.

  • an Array is a fixed length list of items

    • use it whenever you have a fixed set of values you want to process
  • a List is a list of items that will (at least in standard implementations) always try to adapt its capacatiy to the number of items

    • use it whenever you need something more dynamic than an Array

So, in conclusion you will get a better outcome by switching the usage of these to structures (you'll se why below).

// we exactly know our initial input, so we can use an Array here
Integer[] array = {heure, minute, seconde, jour, mois, year};

// we don't know how many items will go in here, so we use a List for now
ArrayList<Integer> list = new ArrayList<>();

// you can loop over an Array just as over a List
for (int value : array) {
    // TODO do your calculations and save them in this.result

    // the ArrayList provides this neat utility to add an element
    // it will automatically increase its capacity, if required
    list.add(this.result);
}
tvGetdata.setText(list.toString());

Of course you can replace the array variable with another List and set the elements from a List to a List. The basic construct is just the same.

6 Comments

thank u but it display empty list, no value was inserted !
it display always the last data ( year) and not the 6 together
No idea what could be wrong, it works for me. Could you please show how you implemented my example, e.g. by editing your question?
@D.G You are looping over the empty List, not over the populated Array.
@Izuro sorry i didn't understand what do you mean :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.