List with anything?

Discussion in 'Spigot Plugin Development' started by GamemodeSurvival, Oct 10, 2019 at 3:15 AM.

  1. How can I make a list that contains anything?
    Instead of List<String> the list could contain a integer or whatever
     
  2. List<Integer>
     
    • Winner Winner x 3
  3. Considering a List can have any T type, you should read these docs.
     
  4. Neither of you answered what im asking.
     
  5. You don't want to make one single list contain a lot of things, the easier way is to create multiple list of what you want to do

    But if you want to do a single list containing multiple things go with
    List<Object> list = new List<>
    for(object obj : list) {
    if(obj instanceof String) {
    //
     
    #5 Akkrite, Oct 10, 2019 at 3:28 AM
    Last edited: Oct 10, 2019 at 5:03 AM
    • Agree Agree x 1
  6. Not for what I'm doing.
     
  7. List<Object> will allow you to put anything you want into it. that directly answers your question so if its not your answer, ask a better question
     
    • Agree Agree x 4
  8. Strahan

    Benefactor

    Your question, as it was initially worded, sounds like you wanted to know if you can make a List of datatypes other than String which Akkrite answered. So since that wasn't to your satisfaction, one assumes you mean a List where one element is a String, another may be a Player, yet another could be a UUID, etc. Akkrite's second answer covered that, though he was doing an instanceof check on the wrong part.

    So why don't you make what you want a bit more clear; give us details rather than vague crap like "Not for what I'm doing". You didn't think that maybe it would help if you told us what you are doing? ;)
     
    • Agree Agree x 2
    • Winner Winner x 2
    • Funny Funny x 1
  9. Ooops didn't see that I put it in the wrong place, was just a quick write up!
     
  10. So... what are you doing? How is anyone supposed to answer your question?
     
  11. Clearly we're supposed to be mind readers /s
     
    • Agree Agree x 2
  12. Now, your question has already been answered, and people are derailing a bit.

    BUT, my question to you is what this would be useful for. A very important concept for OOP is inheritance. I have never, ever needed a list of object of any kinds, as they can often be fitted into a list by using their super classes. This question is not to mock you, but there might be a better approach to what you are currently trying to achieve ;)
     
    • Agree Agree x 1
  13. He said make a list of each type, I said that is not helpful for what I need it for. Also I said make a list that can hold anything, I even gave an example.
     
  14. I am making a config manager class and I need a .setList()
     
  15. Just use the Yamlconfiguration or directly snakeyaml. Do not brew your own beer.
     
    • Agree Agree x 1
  16. "list could contain a integer or whatever"
    Good explanation man, and example! /s
     
  17. Isn't he just asking for
    Code (Text):
    List<Object>();
    So it can store all the objects like String, Integer, Boolean e.t.c.
     
  18. oh, youre wanting a method that allows the input of any List type?
    List<?> in an argument allows you to pass any List into that argument
     
  19. Yes, this is all I was asking for, Thanks, and to everyone hating, stop cutting stuff out xD I literally put instead of List<String> and List<Integer> one list could contain both.
     
  20. List<Object> can contain both. you just didn't know what you were looking for and didnt elaborate your issue enough to get a valid answer.
     
    • Agree Agree x 2