Java的动态数组 [英] Java Dynamic arrays

查看:132
本文介绍了Java的动态数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个Java的编程类,我需要用动态数组帮助。我环顾四周,不能想办法做到这一点是我的简单的水平。我相信在不远的类和刚刚学会的基础知识,所以我不知道多少,但我需要知道如何让一个动态数组。

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;
    }
}
`

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) );
        }
    }
}

第二个是应该继承的第一个,让你一旦被输入创建多个阵列,但是当我使用这些它说我有一个错误,它说,类名 ReverseWithDynamicArray 如果明确要求标注处理只能接受。

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.

推荐答案

使用您的第一个示例程序,我在System.arraycopy改变你的参数

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;
}
}

这篇关于Java的动态数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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