-1

Lets assume that I store values to List A, List B & List C through for loop. List A,B,C values is added by the following way:

for (int j = 0; j <5; j++) 
{
   list_A.add("Value"+i);
   list_B.add("Value"+i);
   list_C.add("Value"+i);
}

To access the List out of the method I'm using this keyword by the following way:

private CharSequence ListA[]; // declaration    
this.ListA= list_A.toArray(new CharSequence[list_A.size()]); //accessing with this keyword

Now I am adding all these list to another List by the following way

List<List<String>> finallist = new ArrayList<>();
finallist .add(list_A);
finallist .add(list_B);
finallist .add(list_C);

I did some searching but I couldn't find how to declare and access List< List< String>> using this keyword?

Note: It is the actual modification of the question that I have asked here. As I couldn't get any answer, I thought I could make it bit simple and have raised the question here.

Community
  • 1
  • 1
sri
  • 31
  • 3
  • Declarations have nothing to do with the `this` keyword, and where you are using it here it is redundant. – user207421 Feb 10 '17 at 06:02
  • I'm using it for fragment. I have asked a question already in SO http://stackoverflow.com/questions/42137823/how-to-get-listliststring-with-this-keyword?noredirect=1#comment71442116_42137823 – sri Feb 10 '17 at 06:05
  • 'Using it for fragment' doesn't make any more sense than the rest of the question, which is a duplicate. Don't do that. – user207421 Feb 10 '17 at 06:07

4 Answers4

0

You use the this keyword to access a member of method of the current instance of the class. You would only do that inside of a function- because outside of a function the this keyword isn't defined.

Generally you don't actually use this.variable or this.method. You only need to do that if another variable at overriding scope uses the same name. Generally you see this in autogenerated setters/constructors and not much else- its easier just not to reuse the name.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • Please look into this , as i need to access it outside for fragment : http://stackoverflow.com/questions/42137823/how-to-get-listliststring-with-this-keyword?noredirect=1#comment71442116_42137823 – sri Feb 10 '17 at 06:06
  • The this keyword has absolutely nothing to do with what you want. You need to learn to ask better questions. – Gabe Sechan Feb 10 '17 at 06:08
0

When accessing a variable of one class in another, you could do something like this:

Main Class

public static final List<String> list_A = new ArrayList<>();
static{
    for (int j = 0; j <5; j++) 
    {
        list_A.add("Value"+i);
    }
}

Fragment Class

CharSequence ListA[];
ListA = MainClass.list_A.toArray(new CharSequence[MainClass.list_A.size()]);

I would recommend using the static keyword in the declaration of list_A

Mick Ashton
  • 356
  • 4
  • 15
  • Someone beat me to it. – Mick Ashton Feb 10 '17 at 06:01
  • I need to use it for fragment. Have explained it in detail here : http://stackoverflow.com/questions/42137823/how-to-get-listliststring-with-this-keyword?noredirect=1#comment71442116_42137823 – sri Feb 10 '17 at 06:04
  • I don't understand why these people are just saying "Your question sucks" and not actually helping you. – Mick Ashton Feb 10 '17 at 06:15
  • I could able to access List A,B,C. But how to access the final list where I have added all the list to that list ? – sri Feb 10 '17 at 06:19
  • Just access it. Use `MainClass.list_A/B/C` to get each list, and then add them to the list in your Fragment, or create the `finallist` in your MainClass and access that the exact same way. – Mick Ashton Feb 10 '17 at 06:22
  • I have done that but I need to pass the Final List to a bundle , so i need to know how to get the List< List< String>> . Can u please refer the link which I have given in Note in my question and tell me how would I pass latest_list to the bundle ? – sri Feb 10 '17 at 06:27
  • http://stackoverflow.com/questions/28190847/how-to-passing-list-in-bundle – Mick Ashton Feb 10 '17 at 06:30
  • Thanks for the reference but I can already pass the List to the bundle. I just want to know how to pass < List < List >> also the Line that should come after Line no 18 in my actual question. How would I assign it in this keyword . – sri Feb 10 '17 at 06:41
  • You don't. I can only recommend that you take Android Java online courses or watch YouTube videos. You'll learn more by looking, not asking. – Mick Ashton Feb 10 '17 at 06:44
  • I have edited my actual question. Now can you please help me with (1) and (2) which I have marked as comments – sri Feb 10 '17 at 06:45
0

You need to display your code, because the scopes really matter in this case. Whether or not you need the this keyword is highly dependent on where things are declared.

bremen_matt
  • 6,902
  • 7
  • 42
  • 90
0

this.ListA means that you want to access field ListA of this object.
If you declare a variable as List<List<String>> finallist = new ArrayList<>(); inside some method, then it is not a field of this object, and you can't access it with this keyword. If you want to access it outside the method, you should declare it as a class field.

It is Java-very-basics thing about classes, fields and this. You should learn Java very basics to eliminate hundreds of questions like this, on which you will loose a lot of time. Much more than to learn very basics.

Vladyslav Matviienko
  • 10,610
  • 4
  • 33
  • 52