演示Java中的有无static修饰的方法,变量

标题:演示Java中的有无static修饰的方法,变量

/**
 * 这个【类里面的方法】访这个类中的实例变量,需要创建一个对象才能访问【可以访问private修饰的,因为在一个类中】
 * 在static修饰的方法里面, 不能出现this,
 *     所以访问非static修饰的方法,实例变量,需要创建一个对象,才能访问
 * 	       访问static修饰的方法,类变量,【默认有一个类名.】,可以省略类名
 * 在没有static修饰的方法里面,
 *     访问没有static修饰的方法,实例变量,可以省略this,【默认有一个this-->当前类的对象】
 *     访问static修饰的方法,类变量,【默认有一个类名.】,可以省略类名
 * 
 * 总的来说,
 *    访问非static修饰的,
 *        在static修饰的方法中:需要手动的创建一个对象,来访问
 *        在非static修饰的方法中:可以使用this.来访问【或者省略this.】
 *    访问static修饰的,默认是【类名.】 可以省略
 * 因为main是static修饰的,所以在main中访问实例变量,没有static修饰的方法,需要【创建一个对象】来访问
 * @param args
 */
public class BSTIterator {
	private List<TreeNode> list;
	private int count;
	
	public BSTIterator(TreeNode root) {
		//初始化list
		list = new ArrayList<>();
		this.inOrder(root);
		
		count = 0;
	}
	
	/**
	 * 中序递归遍历
	 * @return
	 */
	public void inOrder(TreeNode root) {
		if(root == null) {
			return ;
		}else {
			this.inOrder(root.left);
			list.add(root);
			this.inOrder(root.right);
		}
	}
	
	public int next() {
		if(!hasNext()) {
			throw new RuntimeException("数组越界!!!");
		}
		return list.get(count++).val;
	}
	public boolean hasNext() {
		if(count >= list.size()) {
			return false;
		}
		
		return true;
	}
	
	/**
	 * 初始化一个tree
	 * 类广度遍历
	 * @param a
	 * @return
	 */
	public static TreeNode initTree(Integer[] a) {
		if(a == null || a.length == 0) {
			return null;
		}
		
		int t = 0;
		TreeNode p = new TreeNode(a[t]);  //至少有一个元素
		Queue<TreeNode> q = new LinkedList<>();
		q.offer(p);
		
		while(!q.isEmpty()) {
			TreeNode node = q.poll();
			if(t + 1 == a.length) {  //先判断数组中是否还有下一个元素
				return p;
			}else {
				t++;
				if(a[t] == null) {  //若下一个元素为null,则不需要创建新的节点
					node.left = null;
				}else {
					node.left = new TreeNode(a[t]);
					q.offer(node.left);
				}
			}
			if(t + 1 == a.length) {
				return p;
			}else {
				t++;
				if(a[t] != null){  //上面的简写,a[t] == null,不需要再赋值
					node.right = new TreeNode(a[t]);
					q.offer(node.right);
				}
			}
		}
		
		return p;
	}
	
	//Test class should have exactly one public zero-argument counstructor
	//Test class can only have one constructor
	
	/**
	 * 这个【类里面的方法】访这个类中的实例变量,需要创建一个对象才能访问【可以访问private修饰的,因为在一个类中】
	 * 在static修饰的方法里面, 不能出现this,
	 *     所以访问非static修饰的方法,实例变量,需要创建一个对象,才能访问
	 * 	       访问static修饰的方法,类变量,【默认有一个类名.】,可以省略类名
	 * 在没有static修饰的方法里面,
	 *     访问没有static修饰的方法,实例变量,可以省略this,【默认有一个this-->当前类的对象】
	 *     访问static修饰的方法,类变量,【默认有一个类名.】,可以省略类名
	 * 
	 * 总的来说,
	 *    访问非static修饰的,
	 *        在static修饰的方法中:需要手动的创建一个对象,来访问
	 *        在非static修饰的方法中:可以使用this.来访问【或者省略this.】
	 *    访问static修饰的,默认是【类名.】 可以省略
	 * 因为main是static修饰的,所以在main中访问实例变量,没有static修饰的方法,需要【创建一个对象】来访问
	 * @param args
	 */
	public static void main(String[] args) {
		Integer[] a = new Integer[] {1, 2, 3, 4, 5, 9, 10, null, 6, 7, 8, null, null, null, 11};		
		TreeNode head = initTree(a);
		
		BSTIterator it = new BSTIterator(head);
		
		while(it.hasNext()) {
			System.out.println(it.next());
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值