Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在arraylist上使用自己的方法? publicstaticvoidarrayreader(分数[]数组) { System.out.println(“此数组有“+array.length+”个元素”); for(int count=0;count_Java_Arraylist - Fatal编程技术网

Java 如何在arraylist上使用自己的方法? publicstaticvoidarrayreader(分数[]数组) { System.out.println(“此数组有“+array.length+”个元素”); for(int count=0;count

Java 如何在arraylist上使用自己的方法? publicstaticvoidarrayreader(分数[]数组) { System.out.println(“此数组有“+array.length+”个元素”); for(int count=0;count,java,arraylist,Java,Arraylist,这是我编写的代码的一部分。除了使用ArrayList而不是arrays之外,我被告知要做同样的事情。因此我遇到了一个问题。如何才能执行array[count]。read() 我试过了,但没用: public static void arrayReader(Fractions[] array) { System.out.println("This array has " + array.length + " elements"); for(int count

这是我编写的代码的一部分。除了使用ArrayList而不是arrays之外,我被告知要做同样的事情。因此我遇到了一个问题。如何才能执行array[count]。read()

我试过了,但没用:

public static void arrayReader(Fractions[] array)
    {
        System.out.println("This array has " + array.length + " elements");
        for(int count = 0; count < array.length; count++)
        {
            System.out.println("For cell number " + (count+1));
            array[count] = new Fractions();
            array[count].read();
        }
publicstaticvoidreadarray(arraylistarray)
{
System.out.println(“此数组有“+array.size()+”个元素”);
对于(int count=0;count
我不是想让你做作业,我真的被卡住了。所以请帮助我理解这是如何工作的。

你可以使用

public static void readArray(ArrayList<Fractions> array)
    {
        System.out.println("This array has " + array.size() + " elements");
        for(int count = 0; count < array.size(); count++)
        {
            Fractions fraction = new Fractions();
            fraction.read();
            array.set(count, new Fractions());
            array.add(fraction);
        }
    }
上面的一行相当于前面代码中的下一行

array.add(count, new Fractions());
即在ArrayList中的特定索引处添加新分数()

另一行也是如此

array[count] = new Fractions();
你会有

array[count].read();

首先:

(array.get(count)).read();
 array[count] = new Fractions();
不等同于:

(array.get(count)).read();
 array[count] = new Fractions();
arraylist.set
方法:

 array.set(count, new Fractions());
 array.add(fraction);
Replaces the element at the specified position in this list
 with the specified element.
arrayList.add
方法:

 array.set(count, new Fractions());
 array.add(fraction);
Replaces the element at the specified position in this list
 with the specified element.
仅使用

Appends the specified element to the end of this list.
则for循环不同

如果您正在执行以下操作:

array.add(fraction);
array = new ArrayList(5); //initial capacity

 for(int count = 0; count < array.size(); count++)
array=newarraylist(5);//初始容量
对于(int count=0;count
大小是0

如果你正在做:

array.add(fraction);
array = new ArrayList(5); //initial capacity

 for(int count = 0; count < array.size(); count++)
array=新分数[5];//大小
对于(int count=0;count

大小是5。

它是如何工作的?请说明为什么要添加分数并设置分数?这是多余的。对不起,我实际上已经去掉了分数集,并且在发布它时忘记了将其取出。@TeneCursum当我尝试输出代码时。它不打印任何内容。我的问题是下一行。数组[count]的等效值是什么。read();您可以像我上面提到的那样使用。或者您也可以这样做。分数=数组。获取(计数);分数。读取();您必须更改(int count=0;计数<数组。大小();计数++)。谢谢。我有一个问题。我试过了,但没有用。ArrayList数组=新ArrayList(5);这在语法上是否正确?当我检查数组的大小时,它将返回0。这是不正确的。这是初始容量而不是大小。它与数组不同。检查链接:刚刚更新了答案非常感谢。这提供了信息,我现在就知道了。