简体   繁体   中英

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.

Input strings contains only a,b,c,d to make it simple,

when an input comes which is "bcda", then it would go into array 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.

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.

Let say I have input of few strings which based on a,b,c,d.

For ecample, “acdb”,”bcda”,”dbca” and so on.

What I want, when my program receive string "acdb" , it would save like this way.

----------------
| 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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