简体   繁体   English

Java类中的类

[英]class within class in java

EDIT, its really pitty that i can not share the pic but lets try one more time. 编辑,我不能分享图片,但让我再尝试一次,确实很可惜。

i have one array which has 4 portions, first portion take only those strings which starts with a and last one take which starts with d. 我有一个包含4个部分的数组,第一部分仅包含以a开头的字符串,最后一个包含d开头的字符串。

Input strings contains only a,b,c,d to make it simple, 输入字符串仅包含a,b,c,d,以使其变得简单,

when an input comes which is "bcda", then it would go into array 1 . 当输入为“ bcda”时,它将进入数组1 Then next it should pick the array[2] because second char is c and then d array[3] and then array[4] for char a. 然后接下来应该选择array [2],因为第二个字符是c,然后是d array [3],然后是char a的array [4]。

then i would insert this string. 然后我将插入此字符串。 so next time if i want to see that given string let say same "bcda" exists or not, instead of comparing all strings in available set, i will traverse path by using char sequence and then i would know that this string name exists or not. 因此,下次如果我想查看给定的字符串,说是否存在相同的“ bcda”,而不是比较可用集中的所有字符串,我将使用char序列遍历路径,然后我将知道此字符串名称是否存在。

Let say I have input of few strings which based on a,b,c,d. 假设我输入了一些基于a,b,c,d的字符串。

For ecample, “acdb”,”bcda”,”dbca” and so on. 对于ecample,“ acdb”,“ bcda”,“ dbca”等。

What I want, when my program receive string "acdb" , it would save like this way. 我想要的是,当我的程序接收到字符串“ acdb”时,它将像这样保存。

----------------
| a| b | c | d |
----------------
  |
  |
----------------
| a| b | c | d |
----------------
         |
         | 
----------------
| a| b | c | d |
----------------
             |
             | 
----------------
| a| b | c | d |
----------------
     |
     |
and here it can add string in list

quite unfortunate that i cannot add image because of my few points. 很不幸,由于我的几点,我无法添加图像。 :( I hope i explain correctally in self created image. :(我希望我用自己创造的形象正确地解释。

So when I will search any string within these limits then I could trevers easly and find that this string exists or not. 因此,当我搜索这些限制内的任何字符串时,我可以轻松地遍历并发现此字符串是否存在。

I am very confused how to create class within class within class… :( 我很困惑如何在班级内的班级内创建班级……:(

I could create a simple list and add in given string in array. 我可以创建一个简单的列表,然后在数组中添加给定的字符串。 Which is quite simple but could not move forward. 这很简单,但是无法前进。

class MyList {

    TreeSet<String> list = new TreeSet<String>();

}

class Test
{
    MyList Array[] = new MyList [4];

    public Test(){
        for (int k = 0; k < 4; k++){

            Array[k] = new MyList ();
        }
    }
}

If you wanted to create a class within a class , here is how you would accomplish that. 如果您想在一个类中创建一个类 ,请按以下步骤完成。 You might use this when your inner class is closely related to your outer class, but not really used anywhere else. 当您的内部类与外部类紧密相关但在其他地方没有真正使用时,可以使用此方法。 Or maybe you just wanted to group them together for whatever reason. 或者,也许您出于某种原因只想将它们组合在一起。

class OuterClass {

    class InnerClass {

    }

}

If you want a class to contain itself , here is how you would accomplish that. 如果您想让一个类包含自身 ,请按以下步骤完成。 You might do this to create a list of objects. 您可以执行此操作以创建对象列表。 This seems more like what you are trying to do. 这似乎更像您要尝试执行的操作。

class MyClass {

    MyClass nextInTheList;

    public MyClass() {

    }

    public void setNext(MyClass next) {
        nextInTheList = next;
    }
}

Inner classes are almost irrelevant for your problem. 内部课程与您的问题几乎无关。 Here is what you are really trying to do: 这是您真正想做的事情:

public class CharNode extends AbstractCollection<CharSequence>{
    Map<Character, CharNode> children = new HashMap<>();
    int mark = 0;

    @Override
    public void add(CharSequence s){
        if(s.length()==0){mark++; return}
        if(!children.containsKey(s.charAt(0))
            children.put(s.charAt(0), new CharNode());

        children.get(s.charAt(0)).add(s.subSequence(1,s.length()));
    }

    @Override
    public void remove(CharSequence s){
        if(s.length()==0){mark--; return;}

        CharNode child = children.get(s.charAt(0));
        child.remove(s.subSequence(1,s.length()));

        if(child.isEmpty()) children.remove(s.charAt(0));
    }

    @Override
    public boolean contains(CharSequence s){
        if(s.length()==0) return mark;
        return children.containsKey(s.charAt(0)) &&
               children.get(s.charAt(0)).contains(s.subSequence(1,s.length()));
    }

    @Override
    public int size(){
        int result = mark;
        for(CharNode child: children.values()) result+= child.size();
        return result;
    }

    @Override
    public Iterator<CharSequence> iterator(){
        return new Iterator<CharSequence>(){
            @Override public boolean hasNext(){  /* you do this part */  }
            @Override public CharSequence next(){ /* you do this part */ }
            @Override public boolean remove(){ /* you do this part */ }
        }
    }


}

The very last method, iterator , involves using an anonymous inner class , which is the only kind of inner class that is really important, and the only type I would recommend for use by anyone not intimately familiar with the way inner classes work. 最后一种方法iterator涉及使用匿名内部类 ,这是唯一真正重要的内部类,也是我建议供不熟悉内部类工作方式的任何人使用的唯一类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM