当前位置: 代码迷 >> J2SE >> 如下文:foot变量没有初始化为什么可以进行比较?该如何处理
  详细解决方案

如下文:foot变量没有初始化为什么可以进行比较?该如何处理

热度:51   发布时间:2016-04-24 00:54:15.0
如下文:foot变量没有初始化为什么可以进行比较?
Java code
class Array{    private int temp[]; //整形数组    private int foot ;//定义添加位置    public Array(int len){        if(len>0){            this.temp = new int[len];        }else{            this.temp = new int[1];                }    }    public boolean add(int i){ //增加元素        if(this.foot < this.temp.length){ //还有空间            this.temp[foot] = i;//增加元素            this.foot ++; //修改脚标            return true;        }else{            return false;        }    }    public int[] getArray(){        return this.temp;    }};class SortArray extends Array{    public SortArray(int len){        super(len);    }    public int[] getArray(){        java.util.Arrays.sort(super.getArray());        return super.getArray();    }}class ReverseArray extends Array  //反转操作类{    public ReverseArray(int len){        super(len);    }    public int[] getArray(){        int t[] = new int[super.getArray().length];        int count = t.length - 1;        for(int x=0 ;x<t.length; x++){            t[count] = super.getArray()[x]; //数组反转            count --;        }        return t;    }}public class ArrayDemo{    public static void main(String args[]){        ReverseArray a = null;        a = new ReverseArray(5);        System.out.print(a.add(23) + "\t");        System.out.print(a.add(21) + "\t");        System.out.print(a.add(2) + "\t");        System.out.print(a.add(42) + "\t");        System.out.print(a.add(5) + "\t");        System.out.print(a.add(6) + "\t");        print(a.getArray());    }    public static void print(int i[]){        for(int x=0 ; x<i.length;x++){            System.out.print(i[x] + "、");        }    }}


------解决方案--------------------
这里?
private int foot ;//定义添加位置

成员变量会自动初始化,int型就是0。


你说的“没有初始化”这个概念,只有在局部变量中才会存在。