简体   繁体   English

Java动态数组

[英]Java Dynamic arrays

I am taking a programming class for java and I need help with the Dynamic arrays. 我正在学习Java编程课程,并且需要有关动态数组的帮助。 I have looked around and can't find ways to do so that are on my level of simplicity. 我环顾四周,找不到解决方法,这只是我的简单程度。 I am not far in the class and just learned the basics so I don't know to much but I need to know how to make a Dynamic Array. 我上课不远,只是学习了基础知识,所以我不太了解,但是我需要知道如何制作动态数组。

Here are the two sample Programs we were given: 这是我们获得的两个示例程序:

public class DynamicArrayOfInt
{
    private int[] data;
    public DynamicArrayOfInt()
    {
        data = new int[1];
    }
    public int get(int position)
    {
        if (position >= data.length)
            return 0;
        else 
            return data[position];
        }
    public void put(int position, int value)
    {
        if (position >= data.length)
        {
            int newSize = 2 * data.length;
            if (position >= newSize)
                newSize = 2 * position;
            int[] newData = new int[newSize];
            System.arraycopy(data, 0, newData, data.length);
            data = newData;
            System.out.println("Size of dynamic array increased to " + newSize);
        }
        data[position] = value;
    }
}
`

Number 2 2号

import java.util.Scanner;
public class ReverseWithDynamicArray
{
    public static void main(Sting[] args)
    {
        DyanamicArrayOfInt numbers;
        int numCt;
        int num;
        Scanner scan = new Scanner(System.in);
        numbers = new DynamicArrayOfInt();
        numCt = 0;
        System.out.println("Enter some postive integers; Enter 0 to end");
        while (true)
        { 
            num = scan.nextInt();
            if (num <= 0)
                break;
            numbers.put(numCt, num); 
            numCt++;
        }
        System.out.println("\nYour numbers in reverse order are:\n");
        for (int i = numCt - 1; i >= 0; i--)
        {
            System.out.println( numbers.get(i) );
        }
    }
}

The Second one is supposed to inherit the first and allow you to Create more arrays once they are typed in. But when I use these it says I have an error and it says that Class names ReverseWithDynamicArray are only accepted if annotation processing is explicitly requested. 第二个应该继承第一个,并允许您在键入它们后创建更多的数组。但是当我使用它们时,它说我有一个错误,并且说只有当显式请求注释处理时,类名称ReverseWithDynamicArray才被接受。

use this for your 1st sample program, I changed your parameters at System.arraycopy 将此用于您的第一个示例程序,我在System.arraycopy处更改了参数

public class DynamicArrayOfInt
{
private int[] data;
public DynamicArrayOfInt()
{
    data = new int[1];
}
public int get(int position)
{
    if (position >= data.length)
        return 0;
    else 
        return data[position];
    }
public void put(int position, int value)
{
    if (position >= data.length)
    {
        int newSize = 2 * data.length;
        if (position >= newSize)
            newSize = 2 * position;
        int[] newData = new int[newSize];
        System.arraycopy(data, 0, newData, 0, data.length);
        data = newData;
        System.out.println("Size of dynamic array increased to " + newSize);
    }
    data[position] = value;
}
}

Why are you not trying Collections? 为什么不尝试收藏? As I think LinkedList is best for it. 我认为LinkedList最适合它。 Though I am not that much sure about your requirement. 尽管我对您的要求不是很确定。 I am trying to put some sample code here: 我正在尝试将一些示例代码放在这里:

//create a LinkedList object :
LinkedList ll=new LinkedList();

//Add your items in linked list as many as you like
ll.add("item");// you can also add on a specific position by using ll.add(index, item);

//for getting the length of your LinkedList use:
int size=ll.size();

//for reversing the list items use :

Collections.reverse(list);//or you can manually implement it by using size or length of list

/* for printing the list, simply put it in Sop
(As toString method is overriden in Collection Framework to give a output string in
the form like: [collection items separated with comma] ) */

//Note: The difference between ArrayList and Linkedlist is that ArrayList implements RandomAccess interface, so it provides constant access time for accessing any random index. ///注意:ArrayList和Linkedlist之间的区别在于ArrayList实现RandomAccess接口,因此它为访问任何随机索引提供了恒定的访问时间。 So using ArrayList for retrieval is best but for insertion in a random position ArrayList is not suitable, as it needs resize of ArrayList and several shift operations. 因此,最好使用ArrayList进行检索,但不宜将其插入随机位置,因为ArrayList需要调整ArrayList的大小和若干移位操作。

LinkedList is implemented for sequential access in the form of nodes with doubly linked list. LinkedList实现为具有双链表的节点形式的顺序访问。 For accessing any random index it'll need to access next address upto that node. 要访问任何随机索引,将需要访问到该节点的下一个地址。 So for random retrieval/reading LinkedList is not suitable. 因此,对于随机检索/读取,LinkedList不适合。 But for insertion on a random index it just needs to maintain a new node to be inserted. 但是要在随机索引上插入,只需要维护一个要插入的新节点即可。 So for insertion in between or any where in your list LinkedList is suitable. 因此,适合在列表之间或列表中任何位置插入LinkedList。

I hope it may help you. 希望对您有帮助。

You're missing one argument in System.arraycopy(),Following is the declaration for java.lang.System.arraycopy() method 您在System.arraycopy()中缺少一个参数,以下是java.lang.System.arraycopy()方法的声明

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

src -- This is the source array. src-这是源数组。

srcPos -- This is the starting position in the source array. srcPos-这是源数组中的起始位置。

dest -- This is the destination array. dest-这是目标数组。

destPos -- This is the starting position in the destination data. destPos-这是目标数据中的起始位置。

length -- This is the number of array elements to be copied. length-这是要复制的数组元素的数量。

Have a look at primitive implementation of collection in java. 看看Java中collection的原始实现。 There are many libraries available. 有许多可用的库。 One of the good implementation is Trove 很好的实现之一是Trove

I hope you can save space and time both using primitive collections. 我希望您可以使用原始集合节省空间和时间。

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

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