数组问题[JAVA] [英] Problem with array [JAVA]

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

问题描述

我的程序有一个很大的问题。该程序由菜单构成,每个选择都有一个类。在Vettori课程中,当我输入数字时,我遇到了这个问题。对不起,我的英文不好,但我是意大利人。



我的尝试:



这是错误:

Hi, I have a BIG problem with my program. This program is structured with a menu and it has a class for every choise. In the class Vettori I have this problem when I push in the numbers. Sorry for my bad english but I'm Italian.

What I have tried:

this is the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0<br />
	at generale.Vettori.Caricamento(Vettori.java:39)<br />
	at generale.Generale.main(Generale.java:43)<br />
C:\Users\Peppe7\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1<br />
BUILD FAILED (total time: 4 seconds)





这是一个类:



this is a class:

package generale;
import java.io.*;

/**
 *
 * @author DE_VITA1700
 */
  public class Vettori {
        //*******************************************
        //Input
        InputStreamReader input = new InputStreamReader(System.in);
        BufferedReader tastiera = new BufferedReader(input);
        //******************************************* 
     private int N;
     
     public void setN(int x){
        N=x;
     }
     private int V[] = new int[N];
     public void Caricamento() {
         int dato=0;
         String numero;
       
         for(int i=0;i<N;i++) {
            try {
             System.out.print("Valore da inserire alla posizione n°" + i + ": ");
             numero = tastiera.readLine();
             dato = Integer.valueOf(numero).intValue();
            }
            catch(Exception e){
            System.out.println("Inserisci un numero!");
            }
            V[i]=dato;
         }
     }
         public void Stampa(){
         for(int i=0;i<V.length;i++){
             System.out.println(V[i]);
         }
     }
  
  
  
  }

推荐答案

N是类级整数,因此它在实例化类时获取默认值。

V是类级数组,当类被实例化为N个元素的数组时会初始化。

所有这些都在调用类构造函数之前发生,因此构造函数可以使用变量。



由于N是默认的,它是为零,因此V初始化为零元素数组。任何访问数组的任何元素的尝试都将失败并出现Index out of bounds错误,因为数组中没有元素!



稍后更改N通过你的 setN 方法点不会导致数组被重新初始化,你必须自己作为setter的一部分。
N is a class level integer, so it gets the default value when the class is instantiated.
V is a class level array, which is initialized when the class is instantiated to an array of N elements.
All of this happens before the class constructor is called, so that the constructor can use the variables.

Since N is defaulted, it's zero, so V is initialized to an array of zero elements. Any attempt to access any element of the array will fail with an "Index out of bounds" error because there are no elements in the array!

Changing N at a later point via your setN method won't cause the array to be re-initialised, you will have to do that yourself as part of the setter.


Quote:

在Vettori课程中,当我输入数字时,我遇到了这个问题。

In the class Vettori I have this problem when I push in the numbers.



你的代码没有你想象的那样,或者你不明白为什么!



有一个几乎通用的解决方案:在调试器步骤上运行你的代码逐步检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

没有魔法在调试器中,它不知道你的代码应该做什么,它没有找到错误,它只是通过向你显示正在发生的事情来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]



http://docs.oracle .com / javase / 7 / docs / technotes / tools / windows / jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


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

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