Learner
Learner

Reputation: 81

How to store ArrayList element into Array in Java?

I am new to Java and was wondering if anyone would help me out with the logic below. What am trying to do is to iterate over a list and store each list element into Array. Once that's done, then I want to check the array element for correct answers. So for example 3,2,1 is enter then the final score should be 3. Here is the code.

Code

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

public class IterateOverArray {

public static int SCORE = 0;
static List<Integer> list = new ArrayList<Integer>();
static int[] temp;
public static void main(String[] args) throws Exception {

    list.clear();
    list.add(3);
    list.add(2);
    list.add(1);
    getScore();
}

public static void getScore() throws RemoteException {

    temp = new int[3];

    for (Integer value : list) {
        for (int i = 0; i < temp.length; i++) {
            temp[i] += value.intValue();
        }
        if (temp[0] == 3) {
            SCORE++;
        }
        if (temp[1] == 2) {
            SCORE++;
        }
        if (temp[2] == 1) {
            SCORE++;
        }
    }
    System.out.println("total score: " + SCORE); // Final Score should be 3!
}
}

Many Thanks!

Upvotes: 0

Views: 10336

Answers (3)

Lukas Knuth
Lukas Knuth

Reputation: 25755

When in doubt with an algorithm: Mental Step-through!

  1. call getScore()
  2. new array tmp, all elements initialized to 0
  3. first iteration, value of value is 3
  4. add 3 to all elements in the array (makes all of them 3)
  5. check if first element in array is 3 --> it is, increase Score
  6. next iteration, value of value is now 2
  7. add 2 to all Elements of the array (makes all of them 5)
  8. ...

You see your flaw here?

The values in tmp will grow higher, and will then never evaluate to true in your if-statements. Did you want to use = instead of += in your inner for-loop?


Here is a sample-implementation:

static List<Integer> list = new ArrayList<Integer>();
// ...
list.add(3);
list.add(2);
list.add(1);
getScore();

public static void getScore() throws RemoteException {
    if (list.get(0) == 3) {
        SCORE++;
    }
    if (list.get(1) == 2) {
        SCORE++;
    }
    if (list.get(2) == 1) {
        SCORE++;
    }
    System.out.println("total score: " + SCORE);
}

A List is just a dynamically growing array. It behaves just like an array and you can get a single item from it by using the get(index)-method (which is the "equivalent" to using [index]).

You don't need to copy your contents, at all. But if you must use an array, you can convert a List to an array by using the List.toArray()-method.

Upvotes: 2

Sophistifunk
Sophistifunk

Reputation: 5032

I'd just put the correct answers in an array, and compare them as you go. No need to transform things into arrays and whatnot.

int getScore(List<Integer> answers) {
    Integer[] correctAnswers = {3,2,1};
    int score = 0;
    for (int i = 0; i < 3; i++) 
        if (correnctAnswers[i].equals(answers.get(i)))
            score++;
    return score;
}

Upvotes: 1

package farzi;

import java.util.ArrayList;

public class ArrayListToArray 
{
    public static void main (String args[])
    {
        ArrayList<String> arrList = new ArrayList<String>();
        arrList.add("hussi1");
        arrList.add("hussi2");
        arrList.add("hussi3");
        String[] strArray = new String[arrList.size()];
        arrList.toArray(strArray);
        for(int i=0;i<strArray.length;i++)
        {
            System.out.println(strArray[i]);
        }
    }

}

Upvotes: 0

Related Questions