如何创建和在Java中使用稀疏数组通过ArrayList的? [英] How do I create and use a sparse array through ArrayList in Java?

查看:481
本文介绍了如何创建和在Java中使用稀疏数组通过ArrayList的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这个整天,所以和其他地方的研究上,并乱投医我能想到的。我只用Java 2个星期,所以我现在是超级新的这个和完全糊涂了。我不断收到错误,当我尝试使用ArrayList来创建一个稀疏阵列不确定的长度(我拿来自用户的输入,以确定相应的密集阵和密度的长度)。

I've been at this all day, researching on SO and elsewhere and trying everything I can think of. I've only been using java for 2 weeks now so I'm super new to this and totally confused. I keep getting errors when I try to use ArrayList to create a sparse array with an uncertain length (I take input from the user to determine the length of a corresponding dense array and the density).

ArrayList<Double> SparseArray = new ArrayList<Double>[]{
//private ArrayList<ArrayList<Double>> SparseArray;
    for (int i = 0; i < DenseArray.length; i++){
        if (DenseArray[i] != 0){
            SparseArray.add(DenseArray[i]);

我的code的前两行是两个不同的东西我已经试过,这两者都与以下错误:

The first two lines of my code are two different things I've tried, both of which come up with the following errors:

Multiple markers at this line
- Syntax error, parameterized types are only available if source level is 1.5 
 or greater
- Syntax error, parameterized types are only available if source level is 1.5 
 or greater
- Syntax error, insert ";" to complete FieldDeclaration
- Syntax error, insert "ArrayInitializer" to complete 
 ArrayCreationWithArrayInitializer

(编辑:?在ArrayInitializer错误是新的,我从来没有见过,到现在为止如果能解决我的问题,我怎么去使用它)

( the ArrayInitializer error is new, I hadn't seen that until now. If that will solve my problems, how do I go about using it?)

甚至我的进口的java.util.ArrayList;给了我这样的:

even my "import java.util.ArrayList;" gives me this:

Multiple markers at this line
- Syntax error on token(s), misplaced 
 construct(s)
- Syntax error on token ".", = expected

任何想法,将大大AP preciated! :)

Any ideas would be tremendously appreciated! :)

编辑:我得到的照顾编译器的问题。我仍然无法弄清楚如何得到它的认可DenseArray作为一个变量(它被用来只是上述相同的功能,除了它现在处理的ArrayList作为一个单独的功能)。我来自哪里的Python我只是默认为全局变量,当这些问题发生的世界,但我不知道在用java做..有一个Java全局变量相同或类似的东西?

I got the compiler issue taken care of. I still can't figure out how to get it to recognize DenseArray as a variable (it was used just above in the same function except it's now treating ArrayList as a separate function). I come from the world of Python where I would just default to global variables when these problems happened, but I don't know what to do in java.. is there a java global variable equivalent or something similar?

推荐答案

错误消息

- Syntax error, parameterized types are only available if source level is 1.5 or greater

可能是由于在Eclipse编译器符合性水平的不正确的设置。

might be due to an incorrect setting of the compiler compliance level in Eclipse.

Menu: Window -> Preferences (at the bottom of the menu)

在弹出的窗口中,点击的Java ,这将打开一个子列表,在这里你点击编译器

In the pop-up window, click on Java, which opens a sub-list, where you click on Compiler.

有,选择编译器符合水平来显示1.7(或1.6)。

There, select "Compiler compliance level" to show 1.7 (or 1.6).

这篇关于如何创建和在Java中使用稀疏数组通过ArrayList的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆