快捷导航

赋值问题,菜鸟求助大神啊,这代码什么问题?


为什么这里的combArray是空的而下面

这里明明给combArray赋值了呀

源代码如下:


import java.util.ArrayList;
import java.util.List;

public class FindCombination {
        private int begin, end;// 组合最小、大长度
        // number长度的组合集合
        private List combArray = new ArrayList();
        // begin-end长度的组合集合
        private List combBalls = new ArrayList();

        public FindCombination(CombNode[] array, int begin, int end) {//初始器
                this.begin = begin;
                this.end = end;
                if (array == null || array.length == 0 || begin > end) {
                        System.out
                                        .println("wrong,the reason maybe is :\"array==null\" or \"array.length == 0\" or \"begin>end\"");
                        return;
                }
                List list = new ArrayList();
                for (int length = begin; length  end) {
                        System.out
                                        .println("wrong,the length must be greater than begin and less than end at the same time");
                        return null;
                }
                return combBalls.get(length);
        }

        public void printCombination() {// 打印所有组合
                for (int i = 0; i < combBalls.size(); i++) {
                        for (int j = 0; j < combBalls.get(i).size(); j++) {
                                System.out.print(combBalls.get(i).get(j).getValue() + " ");
                        }
                        System.out.println();
                }
        }

        public static void main(String[] args) {
                String[] array = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };
                // int[] array={1,2,3,4,5,6,7,8,9,10};
                CombNode[] cn = new CombNode[10];
                for (int i = 0; i < array.length; i++) {
                        cn = new CombNode(array, true);
                }
                System.out.println("\n***********");
                FindCombination fc = new FindCombination(cn, 1, 2);
                System.out.println("***********");
                System.out.println("通过方法打印");
                fc.printCombination();
        }
}

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员1

你每次将数据放入list的后,此时你的list地址值传给combArray,即此时的combArray就是List,然后你将list的数据删除,嗯,combArray与list指向同一块内存区域,也就是所combArray也删除了数据,最后你的combArray和list依旧指向同一块数据,此时的list为空,comAbrray当然也为空了.最后没有数据,你想输出什么,再就是没搞懂的是,你就是想验证个赋值问题,用得着敲这么长的代码作死吗?基本没人想看...还有,即使你不把combArray.clear()清空,也没有数据的,因为之前就已经没有数据了.觉得自己递归写的好,那就去算法区,不想说你了!!!
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP