danamic array Java,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.

回答1:

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;

}

}

回答2:

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.

回答3:

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.

回答4:

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.

来源:https://stackoverflow.com/questions/15891395/java-dynamic-arrays

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值