简体   繁体   English

如何在Java中将ArrayList元素存储到Array中?

[英]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. 我是Java的新手,我想知道是否有人可以通过以下逻辑帮助我。 What am trying to do is to iterate over a list and store each list element into Array. 试图做的是遍历一个列表,并将每个列表元素存储到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. 因此,例如输入3,2,1,则最终分数应为3。这是代码。

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! 非常感谢!

When in doubt with an algorithm: Mental Step-through! 如果对算法有疑问,请执行以下操作: 精打细算!

  1. call getScore() 调用getScore()
  2. new array tmp , all elements initialized to 0 新数组tmp ,所有元素初始化为0
  3. first iteration, value of value is 3 第一次迭代, value3
  4. add 3 to all elements in the array (makes all of them 3) 向数组中的所有元素加3 (使它们全部变为3)
  5. check if first element in array is 3 --> it is, increase Score 检查数组中的第一个元素是否为3 >是,增加分数
  6. next iteration, value of value is now 2 下一次迭代,值的value现在是2
  7. add 2 to all Elements of the array (makes all of them 5 ) 添加 2到阵列中的所有元素(使所有的人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. tmp的值将提高,并且在if -statements中永远不会评估为true Did you want to use = instead of += in your inner for-loop? 您是否要在内部for循环中使用=而不是+=


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. List只是一个动态增长的数组。 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] ). 它的行为就像一个数组,您可以通过使用get(index) -方法(与使用[index]等效” get(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 . 但是,如果必须使用数组,则可以使用List.toArray() - List.toArray()List转换为数组。

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;
}
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]);
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM