java中的动态数组
我想做的是java中的动态数组,java,arrays,Java,Arrays,我想做的是 ... int sum[]; ... for(int z.....){ ... sum[z] = some_random_value; ... } 但它在第sum[z]=ran行给出了一个错误该变量sum可能尚未初始化 我尝试了int sum[]=0而不是int sum[]但即使这样也会出现错误。 (我基本上是一名C程序员)这是为了消除编译时错误: 但是,为了防止运行时错误,我强烈建议您如下初始化阵列: int[] sum = new int[10]; 括号中的
...
int sum[];
...
for(int z.....){
...
sum[z] = some_random_value;
...
}
但它在第sum[z]=ran行给出了一个错误
该变量sum
可能尚未初始化
我尝试了int sum[]=0代码>而不是int sum[]代码>但即使这样也会出现错误。
(我基本上是一名C程序员)这是为了消除编译时错误:
但是,为了防止运行时错误,我强烈建议您如下初始化阵列:
int[] sum = new int[10];
括号中的数字表示数组大小
如果您的大小是动态的,则使用列表
实现,例如ArrayList
int sum[]= new int[length];
你还没有初始化。到现在为止,你刚刚宣布
不要让数组
的长度在初始化时决定
即使您执行intsum[]=null
当您执行sum[z]=ran时,您将得到一个NullPointerException
代码>
我不能保持动态吗?长度是可变的
不可以。在初始化时,数组长度应该是固定的。查看java中的集合。更具体地说,是一个带有实现的列表
接口,它是
列表接口的可调整大小的数组实现。实现所有可选的列表操作,并允许所有元素,包括null
通过写入int[]anArray=newint[10]代码>你是这么说的
分配一个内存足以容纳10个整数元素的数组,并将该数组分配给anArray变量
似乎您对array甚至java都是新手。本教程可能会帮助您更好地理解。
动态大小的数组在Java中是不可能的-您必须在声明它之前知道它的大小,或者在数组上执行调整大小的操作(这可能会很痛苦)
相反,请使用ArrayList
,如果需要将其作为数组,可以将其转换回
List<Integer> sum = new ArrayList<>();
for(int i = 0; i < upperBound; i++) {
sum.add(i);
}
// necessary to convert back to Integer[]
Integer[] sumArray = sum.toArray(new Integer[0]);
List sum=new ArrayList();
for(int i=0;i
如果您谈论的是动态数组,则类可以表示为-
public class DynArray {
private int size; // The current size of the array (number of elements in the array)
private int maxSize; // Size of memory allocated for the array
private Object[] array; // size of array == maxSize
/**
* An array constructor
* Argument specifies how much memory is needed to allocate for elements
*
* @param sz
* @throws IndexOutOfBoundsException
*/
public DynArray(int sz) throws IndexOutOfBoundsException {
// Here called another more general constructor
this(sz, sz, null);
}
/**
* Call the constructor, in which indicated how much memory is allocated
* for the elements and how much memory is allocated total.
*
* @param sz
* @param maxSz
* @throws IndexOutOfBoundsException
*/
public DynArray(int sz, int maxSz) throws IndexOutOfBoundsException {
// Here called another more general constructor
this(sz, maxSz, null);
}
/**
* Additional argument contains an array of elements for initialization
*
* @param sz
* @param maxSz
* @param iniArray
* @throws IndexOutOfBoundsException
*/
public DynArray(int sz, int maxSz, Object[] iniArray) throws IndexOutOfBoundsException {
if((size = sz) < 0) {
throw new IndexOutOfBoundsException("Negative size: " + sz);
}
maxSize = (maxSz < sz ? sz : maxSz);
array = new Object[maxSize]; // memory allocation
if(iniArray != null) { // copying items
for(int i = 0; i < size && i < iniArray.length; i++) {
array[i] = iniArray[i];
// Here it was possible to use the standard method System.arraycopy
}
}
}
/**
* Indexing
*
* @param i
* @return
* @throws IndexOutOfBoundsException
*/
public Object elementAt(int i) throws IndexOutOfBoundsException {
if (i < 0 || i >= size) {
throw new IndexOutOfBoundsException("Index" + i +
" out of range [0," + (size - 1) + "]");
}
return array[i];
}
/**
* Changing the current size of the array. argument delta specifies
* direction of change (positive - increase the size;
* negative - decrease the size)
*
* @param delta
*/
public void resize(int delta) {
if (delta > 0) enlarge(delta); // increasing the size of the array
else if (delta < 0) shrink(-delta); // decreasing the size of the array
}
/**
* Increasing the size of the array
*
* @param delta
*/
public void enlarge(int delta) {
if((size += delta) > maxSize) {
maxSize = size;
Object[] newArray = new Object[maxSize];
// copying elements
for(int i =0; i < size - delta; i++)
newArray[i] = array[i];
array = newArray;
}
}
/**
* Decreasing the size of the array
*
* @param delta
*/
public void shrink(int delta) {
size = (delta > size ? 0 : size - delta);
}
/**
* Adding a new element
* (with a possible increasing the size of the array)
*
* @param e
*/
public void add(Object e) {
resize(1);
array[size-1] = e;
}
/**
* Removing the given value - shifting elements and subsequent
* reduction the size of the array
*
* @param e
*/
public void remove(Object e) {
int j;
for(j = 0; j < size; j++) {
if(e.equals(array[j])) {
break;
}
}
if(j == size) {
return false;
} else {
for(int k = j; k < size; k++)
array[k] = array[k + 1];
resize(-1);
return true;
}
}
}
公共类DynArray{
private int size;//数组的当前大小(数组中的元素数)
private int maxSize;//为数组分配的内存大小
私有对象[]数组;//数组大小==maxSize
/**
*数组构造函数
*参数指定为元素分配所需的内存量
*
*@param sz
*@throws IndexOutOfBoundsException
*/
公共DynArray(int sz)抛出IndexOutOfBoundsException{
//这里称为另一个更通用的构造函数
这(sz,sz,null);
}
/**
*调用构造函数,其中指示分配了多少内存
*对于元素和分配的内存总量。
*
*@param sz
*@param maxSz
*@throws IndexOutOfBoundsException
*/
公共DynArray(intsz,intmaxsz)抛出IndexOutOfBoundsException{
//这里称为另一个更通用的构造函数
这(sz,maxSz,null);
}
/**
*附加参数包含用于初始化的元素数组
*
*@param sz
*@param maxSz
*@paramini数组
*@throws IndexOutOfBoundsException
*/
public DynArray(int sz,int maxSz,Object[]inArray)抛出IndexOutOfBoundsException{
如果((size=sz)<0){
抛出新的IndexOutOfBoundsException(“负大小:+sz”);
}
maxSize=(maxSz=大小){
抛出新的IndexOutOfBoundsException(“索引”+i+
“超出范围[0,+(大小-1)+”]”;
}
返回数组[i];
}
/**
*更改数组的当前大小。参数delta指定
*变化方向(正-增大尺寸;
*负数-减小大小)
*
*@param delta
*/
公共空间大小调整(整数增量){
如果(增量>0)放大(增量);//增加数组的大小
else if(delta<0)收缩(-delta);//减小数组的大小
}
/**
*增加数组的大小
*
*@param delta
*/
公共空间扩大(int delta){
如果((size+=delta)>maxSize){
最大尺寸=尺寸;
Object[]newArray=新对象[maxSize];
//复制元素
对于(int i=0;i大小?0:大小-增量);
}
/**
*添加新元素
*(可能会增加阵列的大小)
*
*@param e
*/
公共无效添加(对象e){
调整大小(1);
数组[size-1]=e;
}
/**
*移除给定值-换档元件和后续步骤
*减少数组的大小
*
*@param e
*/
删除公共空间(对象e){
int j;
对于(j=0;jpublic class DynArray {
private int size; // The current size of the array (number of elements in the array)
private int maxSize; // Size of memory allocated for the array
private Object[] array; // size of array == maxSize
/**
* An array constructor
* Argument specifies how much memory is needed to allocate for elements
*
* @param sz
* @throws IndexOutOfBoundsException
*/
public DynArray(int sz) throws IndexOutOfBoundsException {
// Here called another more general constructor
this(sz, sz, null);
}
/**
* Call the constructor, in which indicated how much memory is allocated
* for the elements and how much memory is allocated total.
*
* @param sz
* @param maxSz
* @throws IndexOutOfBoundsException
*/
public DynArray(int sz, int maxSz) throws IndexOutOfBoundsException {
// Here called another more general constructor
this(sz, maxSz, null);
}
/**
* Additional argument contains an array of elements for initialization
*
* @param sz
* @param maxSz
* @param iniArray
* @throws IndexOutOfBoundsException
*/
public DynArray(int sz, int maxSz, Object[] iniArray) throws IndexOutOfBoundsException {
if((size = sz) < 0) {
throw new IndexOutOfBoundsException("Negative size: " + sz);
}
maxSize = (maxSz < sz ? sz : maxSz);
array = new Object[maxSize]; // memory allocation
if(iniArray != null) { // copying items
for(int i = 0; i < size && i < iniArray.length; i++) {
array[i] = iniArray[i];
// Here it was possible to use the standard method System.arraycopy
}
}
}
/**
* Indexing
*
* @param i
* @return
* @throws IndexOutOfBoundsException
*/
public Object elementAt(int i) throws IndexOutOfBoundsException {
if (i < 0 || i >= size) {
throw new IndexOutOfBoundsException("Index" + i +
" out of range [0," + (size - 1) + "]");
}
return array[i];
}
/**
* Changing the current size of the array. argument delta specifies
* direction of change (positive - increase the size;
* negative - decrease the size)
*
* @param delta
*/
public void resize(int delta) {
if (delta > 0) enlarge(delta); // increasing the size of the array
else if (delta < 0) shrink(-delta); // decreasing the size of the array
}
/**
* Increasing the size of the array
*
* @param delta
*/
public void enlarge(int delta) {
if((size += delta) > maxSize) {
maxSize = size;
Object[] newArray = new Object[maxSize];
// copying elements
for(int i =0; i < size - delta; i++)
newArray[i] = array[i];
array = newArray;
}
}
/**
* Decreasing the size of the array
*
* @param delta
*/
public void shrink(int delta) {
size = (delta > size ? 0 : size - delta);
}
/**
* Adding a new element
* (with a possible increasing the size of the array)
*
* @param e
*/
public void add(Object e) {
resize(1);
array[size-1] = e;
}
/**
* Removing the given value - shifting elements and subsequent
* reduction the size of the array
*
* @param e
*/
public void remove(Object e) {
int j;
for(j = 0; j < size; j++) {
if(e.equals(array[j])) {
break;
}
}
if(j == size) {
return false;
} else {
for(int k = j; k < size; k++)
array[k] = array[k + 1];
resize(-1);
return true;
}
}
}