java中的数组对象
我是面向对象编程新手,我需要编写一个代码,读取N个非零数字的列表,并将其存储在向量中。填写后,应打印读取的数据 这是密码java中的数组对象,java,arrays,vector,Java,Arrays,Vector,我是面向对象编程新手,我需要编写一个代码,读取N个非零数字的列表,并将其存储在向量中。填写后,应打印读取的数据 这是密码 import java.io.*; import java.util.*; class Programa1Vectores_Objetos{ int n,nal; int Vector[]= new int[n]; void setvector(int xvector[]){ Random Aleatorio =new
import java.io.*;
import java.util.*;
class Programa1Vectores_Objetos{
int n,nal;
int Vector[]= new int[n];
void setvector(int xvector[]){
Random Aleatorio =new Random();
int nal;
for(int i=0; i<xvector.length; i++){
nal=Aleatorio.nextInt(10);
if (nal==0){
i--;
continue;
}
xvector[i]=nal;
}
}
int getvector(){
return Vector[n];
}
}
class EjecutaPrograma1{
public static void main (String[] args) throws java.io.IOException {
int n=0;
int Vector[]= new int[n];
Programa1Vectores_Objetos Programa1 = new Programa1Vectores_Objetos();
n=Lectura_NumeroVector("");
Programa1.setvector(Vector);
Programa1.getvector();
}
public static int Lectura_NumeroVector (String TxtMsg) throws java.io.IOException{
int X=0;
String teclado;
DataInputStream cadena= new DataInputStream(System.in);
do{
System.out.print("Longitud del Vector: ");
teclado=cadena.readLine();
try{
X=Integer.parseInt(teclado);
if(X<1){
System.out.println("Tienes que escribir un numero mayor a cero");
continue;
}
return X;
}catch(NumberFormatException e){
System.out.println("Escribe un numero");
}
}while(true);
}
}
第25行
int getvector(){
return Vector[n]; //Line 25
}
第47行
Programa1.getvector();
对不起,我的英语不好
谢谢 好的开始
int n=0;
int Vector[]= new int[n];
这段代码将创建一个空数组。尝试使用更大的数字
使用列表的可能更好的方法,例如
如果你真的调试了你的代码,你自己就会发现这一点。因为一开始你似乎不知道有多少输入值,你应该选择
列表而不是数组。它的优点是随着您的输入数据自动增长。所以使用
List<Integer> Vector = new ArrayList<Integer>();
List Vector=new ArrayList();
您需要更改一些附加代码,因为例如,对列表成员的访问在语法上不同于对数组的访问Vector[i]
变为Vector.get(i)
在主方法中初始化大小为0的向量。如果从未将其设置为新大小,请尝试在设置n后向下移动向量的初始化,如下所示:
public static void main (String[] args) throws java.io.IOException {
int n=0;
// int Vector[]= new int[n];
Programa1Vectores_Objetos Programa1 = new Programa1Vectores_Objetos();
n=Lectura_NumeroVector("");
int Vector[]= new int[n]; // n isn't 0 anymore
Programa1.setvector(Vector);
Programa1.getvector();
}
void setvector(int xvector[]){
Random Aleatorio =new Random();
int nal;
for(int i=0; i<xvector.length; i++){
nal=Aleatorio.nextInt(10);
if (nal==0){
i--;
continue;
}
xvector[i]=nal;
}
Vector = xvector; // Vector needs to be set
}
然后在setVector中,您从未设置Vector变量,我相信在setVector的末尾,您应该执行Vector=xvector
,因此它应该如下所示:
public static void main (String[] args) throws java.io.IOException {
int n=0;
// int Vector[]= new int[n];
Programa1Vectores_Objetos Programa1 = new Programa1Vectores_Objetos();
n=Lectura_NumeroVector("");
int Vector[]= new int[n]; // n isn't 0 anymore
Programa1.setvector(Vector);
Programa1.getvector();
}
void setvector(int xvector[]){
Random Aleatorio =new Random();
int nal;
for(int i=0; i<xvector.length; i++){
nal=Aleatorio.nextInt(10);
if (nal==0){
i--;
continue;
}
xvector[i]=nal;
}
Vector = xvector; // Vector needs to be set
}
void setvector(intxvector[]){
Random Aleatorio=新的Random();
内部;
对于(int i=0;i)您的代码甚至没有编译。一个问题是,类EjecutaPrograma1应该是公共的。getvector
的原型应该是int[]getvector()