简体   繁体   中英

Java Dynamic arrays

I am taking a programming class for java and I need help with the Dynamic arrays. 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

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.

use this for your 1st sample program, I changed your parameters at 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. 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. 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.

LinkedList is implemented for sequential access in the form of nodes with doubly linked list. For accessing any random index it'll need to access next address upto that node. So for random retrieval/reading LinkedList is not suitable. 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.

I hope it may help you.

You're missing one argument in System.arraycopy(),Following is the declaration for java.lang.System.arraycopy() method

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

src -- This is the source array.

srcPos -- This is the starting position in the source array.

dest -- This is the destination array.

destPos -- This is the starting position in the destination data.

length -- This is the number of array elements to be copied.

Have a look at primitive implementation of collection in java. There are many libraries available. One of the good implementation is Trove

I hope you can save space and time both using primitive collections.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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