基本上我有一个位置的ArrayList:
ArrayList<WorldLocation> locations = new ArrayList<WorldLocation>();
Run Code Online (Sandbox Code Playgroud)
下面我称之为以下方法:
.getMap();
Run Code Online (Sandbox Code Playgroud)
getMap()方法中的参数是:
getMap(WorldLocation... locations)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我不确定如何将整个列表locations传入该方法.
我试过了
.getMap(locations.toArray())
Run Code Online (Sandbox Code Playgroud)
但getMap不接受,因为它不接受Objects [].
现在,如果我使用
.getMap(locations.get(0));
Run Code Online (Sandbox Code Playgroud)
它会完美地工作......但我需要以某种方式传递所有位置...我当然可以继续添加locations.get(1), locations.get(2)等等但是阵列的大小各不相同.我只是不习惯整个概念ArrayList
最简单的方法是什么?我觉得我现在不想直接思考.
这个用方括号将数字括起来的语法有什么作用?
new Integer[0];
Run Code Online (Sandbox Code Playgroud)
我在我维护的代码库中找到了它,但找不到任何相关文档。它的使用方式如下:
Set<Form> forms = getForms();
List<Form> formsList = Arrays.asList(forms.toArray(new Form[0]))
Run Code Online (Sandbox Code Playgroud) 我正在尝试以Python为基础学习Java,所以请耐心等待.
我正在实现一个Sierat of Eratosthenes方法(我在Python中有一个;尝试将其转换为Java):
def prevPrimes(n):
"""Generates a list of primes up to 'n'"""
primes_dict = {i : True for i in range(3, n + 1, 2)}
for i in primes_dict:
if primes_dict[i]:
num = i
while (num * i <= n):
primes_dict[num*i] = False
num += 2
primes_dict[2] = True
return [num for num in primes_dict if primes_dict[num]]
Run Code Online (Sandbox Code Playgroud)
这是我尝试将其转换为Java:
import java.util.*;
public class Sieve {
public static void sieve(int n){
System.out.println(n);
Map primes = new HashMap();
for(int x = …Run Code Online (Sandbox Code Playgroud)