Topic: 二唯数组的下标不可以是变量吗!!

  Print this page

1.二唯数组的下标不可以是变量吗!! Copy to clipboard
Posted by: 234aini
Posted on: 2004-11-06 12:25

class toString
{
public static void main(String[] args) throws Exception
{
int argc=args.length;
if(argc!=1)
{
System.out.println("Syntax ERROR");
return;
}
//这是将字符变化成数字的方法,用的是整数类型的包装类
int x=Integer.parseInt(args[0]);
int p=x-1;
int q=2*x-1;
//System.out.println(x);
//初始化一个二唯数组,来做为输出的对象

int y[][]=new int[p][q];//note,init work is not easy,you should note many questions
int i=0;
int n=p;
//int j=0;
for(i=0;i<p;i++)
y[p][i]=i+1;
for(i=i;i<q;i++)
y[q][i]=n--;
for(i=0;i<q;i++)
System.out.print(y[p][i]);

}
}

//为什么二唯数组的下标不可以是变量恩?
一运行就报错

2.Re:二唯数组的下标不可以是变量吗!! [Re: 234aini] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-06 12:44

1.二维数组(Array)的下标可以是变量(使用时)
2.但是在生成的(new)时候,下标不可以是变量
3.如果你想动态(并非真正的动态,真正的动态生成存储空间的请您参照其他算法)的决定数组大小,可以

int[][] yourArray;
//
int x;
int y;
// here some caculation
//x=x+2;
//y=7;
yourArray=new int[x][y];

3.Re:二唯数组的下标不可以是变量吗!! [Re: 234aini] Copy to clipboard
Posted by: boynick_1
Posted on: 2004-11-07 17:49

我试了一下,好象也不行啊
class test
{
public static void main(String args[])
{
int[][] ary;
int x;
int y;
x=x+1;
y=7;
ary=new int[x][y];
}
}
报错:
E:\java\test.java:9: variable x might not have been initialized
x=x+1;
^
1 error

4.Re:二唯数组的下标不可以是变量吗!! [Re: boynick_1] Copy to clipboard
Posted by: littledeer1974
Posted on: 2004-11-07 17:52

you need to initialize your x

not

just x=x+1;
x was null,so x=x+1 means x=null+1(still null)

you can try this

x=6;
x=x+1;


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923