New enum with array





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















I need to save enum values (MERIA and SMI in this case) from a list of strings created by split().



Here is what I have:



public AccessLevel getAccess() {
String Access = "MERIA : SMI";
String split = Access.split(" : ");
for (String Acces : split) {
list.add(AccessLevel.valueOf(Acces));
}
return new AccessLevel{AccessLevel.MERIA,AccessLevel.NONE};
}


I want something like this, with the enum parameters from the list saved into an array:



 public AccessLevel getAccess() {
String Access = "MERIA : SMI";
String split = Access.split(" : ");
for (String Acces : split) {
list.add(AccessLevel.valueOf(Acces));
}
return new AccessLevel{list};

}









share|improve this question

























  • idownvotedbecau.se/imageofcode ... well one of many reasons

    – Stalemate Of Tuning
    Jan 3 at 2:15













  • sorry corrected

    – Extazzzy Team corp
    Jan 3 at 2:18











  • OK in that case idownvotedbecau.se/unclearquestion

    – Stalemate Of Tuning
    Jan 3 at 2:20











  • need to be transmitted AccessLevel.MERIA, AccessLevel.NONE to AccessLevel in array

    – Extazzzy Team corp
    Jan 3 at 2:24











  • select several enums from AccessLevel and return them in enum format

    – Extazzzy Team corp
    Jan 3 at 2:29


















-2















I need to save enum values (MERIA and SMI in this case) from a list of strings created by split().



Here is what I have:



public AccessLevel getAccess() {
String Access = "MERIA : SMI";
String split = Access.split(" : ");
for (String Acces : split) {
list.add(AccessLevel.valueOf(Acces));
}
return new AccessLevel{AccessLevel.MERIA,AccessLevel.NONE};
}


I want something like this, with the enum parameters from the list saved into an array:



 public AccessLevel getAccess() {
String Access = "MERIA : SMI";
String split = Access.split(" : ");
for (String Acces : split) {
list.add(AccessLevel.valueOf(Acces));
}
return new AccessLevel{list};

}









share|improve this question

























  • idownvotedbecau.se/imageofcode ... well one of many reasons

    – Stalemate Of Tuning
    Jan 3 at 2:15













  • sorry corrected

    – Extazzzy Team corp
    Jan 3 at 2:18











  • OK in that case idownvotedbecau.se/unclearquestion

    – Stalemate Of Tuning
    Jan 3 at 2:20











  • need to be transmitted AccessLevel.MERIA, AccessLevel.NONE to AccessLevel in array

    – Extazzzy Team corp
    Jan 3 at 2:24











  • select several enums from AccessLevel and return them in enum format

    – Extazzzy Team corp
    Jan 3 at 2:29














-2












-2








-2








I need to save enum values (MERIA and SMI in this case) from a list of strings created by split().



Here is what I have:



public AccessLevel getAccess() {
String Access = "MERIA : SMI";
String split = Access.split(" : ");
for (String Acces : split) {
list.add(AccessLevel.valueOf(Acces));
}
return new AccessLevel{AccessLevel.MERIA,AccessLevel.NONE};
}


I want something like this, with the enum parameters from the list saved into an array:



 public AccessLevel getAccess() {
String Access = "MERIA : SMI";
String split = Access.split(" : ");
for (String Acces : split) {
list.add(AccessLevel.valueOf(Acces));
}
return new AccessLevel{list};

}









share|improve this question
















I need to save enum values (MERIA and SMI in this case) from a list of strings created by split().



Here is what I have:



public AccessLevel getAccess() {
String Access = "MERIA : SMI";
String split = Access.split(" : ");
for (String Acces : split) {
list.add(AccessLevel.valueOf(Acces));
}
return new AccessLevel{AccessLevel.MERIA,AccessLevel.NONE};
}


I want something like this, with the enum parameters from the list saved into an array:



 public AccessLevel getAccess() {
String Access = "MERIA : SMI";
String split = Access.split(" : ");
for (String Acces : split) {
list.add(AccessLevel.valueOf(Acces));
}
return new AccessLevel{list};

}






java enums






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 5:44









Stalemate Of Tuning

542315




542315










asked Jan 3 at 2:13









Extazzzy Team corpExtazzzy Team corp

42




42













  • idownvotedbecau.se/imageofcode ... well one of many reasons

    – Stalemate Of Tuning
    Jan 3 at 2:15













  • sorry corrected

    – Extazzzy Team corp
    Jan 3 at 2:18











  • OK in that case idownvotedbecau.se/unclearquestion

    – Stalemate Of Tuning
    Jan 3 at 2:20











  • need to be transmitted AccessLevel.MERIA, AccessLevel.NONE to AccessLevel in array

    – Extazzzy Team corp
    Jan 3 at 2:24











  • select several enums from AccessLevel and return them in enum format

    – Extazzzy Team corp
    Jan 3 at 2:29



















  • idownvotedbecau.se/imageofcode ... well one of many reasons

    – Stalemate Of Tuning
    Jan 3 at 2:15













  • sorry corrected

    – Extazzzy Team corp
    Jan 3 at 2:18











  • OK in that case idownvotedbecau.se/unclearquestion

    – Stalemate Of Tuning
    Jan 3 at 2:20











  • need to be transmitted AccessLevel.MERIA, AccessLevel.NONE to AccessLevel in array

    – Extazzzy Team corp
    Jan 3 at 2:24











  • select several enums from AccessLevel and return them in enum format

    – Extazzzy Team corp
    Jan 3 at 2:29

















idownvotedbecau.se/imageofcode ... well one of many reasons

– Stalemate Of Tuning
Jan 3 at 2:15







idownvotedbecau.se/imageofcode ... well one of many reasons

– Stalemate Of Tuning
Jan 3 at 2:15















sorry corrected

– Extazzzy Team corp
Jan 3 at 2:18





sorry corrected

– Extazzzy Team corp
Jan 3 at 2:18













OK in that case idownvotedbecau.se/unclearquestion

– Stalemate Of Tuning
Jan 3 at 2:20





OK in that case idownvotedbecau.se/unclearquestion

– Stalemate Of Tuning
Jan 3 at 2:20













need to be transmitted AccessLevel.MERIA, AccessLevel.NONE to AccessLevel in array

– Extazzzy Team corp
Jan 3 at 2:24





need to be transmitted AccessLevel.MERIA, AccessLevel.NONE to AccessLevel in array

– Extazzzy Team corp
Jan 3 at 2:24













select several enums from AccessLevel and return them in enum format

– Extazzzy Team corp
Jan 3 at 2:29





select several enums from AccessLevel and return them in enum format

– Extazzzy Team corp
Jan 3 at 2:29












2 Answers
2






active

oldest

votes


















2














Create a new array using the length split.length.



public AccessLevel getAccess() {
String access = "MERIA : SMI";
String split = access.split("\s*:\s*");
AccessLevel accessLevels = new AccessLevel[split.length];
for (int i = 0; i < split.length; i++) {
accessLevels[i] = AccessLevel.valueOf(split[i]);
}
return accessLevels;
}


Also note that I changed " : " to "\s*:\s*" to cater any number of spaces. Alternatively, you can use String.trim().



And non-final variable names in java start with lower-case letters.





Or you can use the Stream API:-



public AccessLevel getAccess() {
String access = "MERIA : SMI";
return Arrays.stream(access.split(":"))
.map(String::trim)
.map(AccessLevel::valueOf)
.toArray(AccessLevel::new);
}





share|improve this answer

































    3














    problems



    there are a number of problems with your code:





    1. Access should be access

      in java it is convention to type a lower case letter at the start of a variable and an upper case letter at the start of a class name, that way classes en variables are easily distinguishable


    but mainly





    1. list doesn't exist so you should make a variable.


    solutions



    arrays



    public AccessLevel getAccess() {
    String accessesString = "MERIA : SMI";
    String split = accessesString.split(" : ");

    AccessLevel list = new AccessLevel[split.length];

    for (int i = 0; i < split.length; i++) {
    list[i] = AccessLevel.valueOf(split[i]);
    }
    return list;
    }


    List



    you should really use List instead of basic arrays, they're more versatile and easier to use.



    public List<AccessLevel> getAccess() {
    String accessesString = "MERIA : SMI";
    String split = accessesString.split(" : ");

    List<AccessLevel> list = new ArrayList<>();

    for (String access : split) {
    list.add(AccessLevel.valueOf(access));
    }
    return list;
    }


    Streams



    The most elegant solutions in my opinion but maybe a bit more difficult.



    public List<AccessLevel> getAccess() {
    String accessesString = "MERIA : SMI";
    String split = accessesString.split(" : ");

    return Arrays.stream(split)
    .map(AccessLevel::valueOf)
    .collect(Collectors.toList());
    }


    I hope that was helpful!
    greetz






    share|improve this answer
























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015508%2fnew-enum-with-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      Create a new array using the length split.length.



      public AccessLevel getAccess() {
      String access = "MERIA : SMI";
      String split = access.split("\s*:\s*");
      AccessLevel accessLevels = new AccessLevel[split.length];
      for (int i = 0; i < split.length; i++) {
      accessLevels[i] = AccessLevel.valueOf(split[i]);
      }
      return accessLevels;
      }


      Also note that I changed " : " to "\s*:\s*" to cater any number of spaces. Alternatively, you can use String.trim().



      And non-final variable names in java start with lower-case letters.





      Or you can use the Stream API:-



      public AccessLevel getAccess() {
      String access = "MERIA : SMI";
      return Arrays.stream(access.split(":"))
      .map(String::trim)
      .map(AccessLevel::valueOf)
      .toArray(AccessLevel::new);
      }





      share|improve this answer






























        2














        Create a new array using the length split.length.



        public AccessLevel getAccess() {
        String access = "MERIA : SMI";
        String split = access.split("\s*:\s*");
        AccessLevel accessLevels = new AccessLevel[split.length];
        for (int i = 0; i < split.length; i++) {
        accessLevels[i] = AccessLevel.valueOf(split[i]);
        }
        return accessLevels;
        }


        Also note that I changed " : " to "\s*:\s*" to cater any number of spaces. Alternatively, you can use String.trim().



        And non-final variable names in java start with lower-case letters.





        Or you can use the Stream API:-



        public AccessLevel getAccess() {
        String access = "MERIA : SMI";
        return Arrays.stream(access.split(":"))
        .map(String::trim)
        .map(AccessLevel::valueOf)
        .toArray(AccessLevel::new);
        }





        share|improve this answer




























          2












          2








          2







          Create a new array using the length split.length.



          public AccessLevel getAccess() {
          String access = "MERIA : SMI";
          String split = access.split("\s*:\s*");
          AccessLevel accessLevels = new AccessLevel[split.length];
          for (int i = 0; i < split.length; i++) {
          accessLevels[i] = AccessLevel.valueOf(split[i]);
          }
          return accessLevels;
          }


          Also note that I changed " : " to "\s*:\s*" to cater any number of spaces. Alternatively, you can use String.trim().



          And non-final variable names in java start with lower-case letters.





          Or you can use the Stream API:-



          public AccessLevel getAccess() {
          String access = "MERIA : SMI";
          return Arrays.stream(access.split(":"))
          .map(String::trim)
          .map(AccessLevel::valueOf)
          .toArray(AccessLevel::new);
          }





          share|improve this answer















          Create a new array using the length split.length.



          public AccessLevel getAccess() {
          String access = "MERIA : SMI";
          String split = access.split("\s*:\s*");
          AccessLevel accessLevels = new AccessLevel[split.length];
          for (int i = 0; i < split.length; i++) {
          accessLevels[i] = AccessLevel.valueOf(split[i]);
          }
          return accessLevels;
          }


          Also note that I changed " : " to "\s*:\s*" to cater any number of spaces. Alternatively, you can use String.trim().



          And non-final variable names in java start with lower-case letters.





          Or you can use the Stream API:-



          public AccessLevel getAccess() {
          String access = "MERIA : SMI";
          return Arrays.stream(access.split(":"))
          .map(String::trim)
          .map(AccessLevel::valueOf)
          .toArray(AccessLevel::new);
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 3 at 3:08

























          answered Jan 3 at 2:28









          KartikKartik

          4,52231537




          4,52231537

























              3














              problems



              there are a number of problems with your code:





              1. Access should be access

                in java it is convention to type a lower case letter at the start of a variable and an upper case letter at the start of a class name, that way classes en variables are easily distinguishable


              but mainly





              1. list doesn't exist so you should make a variable.


              solutions



              arrays



              public AccessLevel getAccess() {
              String accessesString = "MERIA : SMI";
              String split = accessesString.split(" : ");

              AccessLevel list = new AccessLevel[split.length];

              for (int i = 0; i < split.length; i++) {
              list[i] = AccessLevel.valueOf(split[i]);
              }
              return list;
              }


              List



              you should really use List instead of basic arrays, they're more versatile and easier to use.



              public List<AccessLevel> getAccess() {
              String accessesString = "MERIA : SMI";
              String split = accessesString.split(" : ");

              List<AccessLevel> list = new ArrayList<>();

              for (String access : split) {
              list.add(AccessLevel.valueOf(access));
              }
              return list;
              }


              Streams



              The most elegant solutions in my opinion but maybe a bit more difficult.



              public List<AccessLevel> getAccess() {
              String accessesString = "MERIA : SMI";
              String split = accessesString.split(" : ");

              return Arrays.stream(split)
              .map(AccessLevel::valueOf)
              .collect(Collectors.toList());
              }


              I hope that was helpful!
              greetz






              share|improve this answer




























                3














                problems



                there are a number of problems with your code:





                1. Access should be access

                  in java it is convention to type a lower case letter at the start of a variable and an upper case letter at the start of a class name, that way classes en variables are easily distinguishable


                but mainly





                1. list doesn't exist so you should make a variable.


                solutions



                arrays



                public AccessLevel getAccess() {
                String accessesString = "MERIA : SMI";
                String split = accessesString.split(" : ");

                AccessLevel list = new AccessLevel[split.length];

                for (int i = 0; i < split.length; i++) {
                list[i] = AccessLevel.valueOf(split[i]);
                }
                return list;
                }


                List



                you should really use List instead of basic arrays, they're more versatile and easier to use.



                public List<AccessLevel> getAccess() {
                String accessesString = "MERIA : SMI";
                String split = accessesString.split(" : ");

                List<AccessLevel> list = new ArrayList<>();

                for (String access : split) {
                list.add(AccessLevel.valueOf(access));
                }
                return list;
                }


                Streams



                The most elegant solutions in my opinion but maybe a bit more difficult.



                public List<AccessLevel> getAccess() {
                String accessesString = "MERIA : SMI";
                String split = accessesString.split(" : ");

                return Arrays.stream(split)
                .map(AccessLevel::valueOf)
                .collect(Collectors.toList());
                }


                I hope that was helpful!
                greetz






                share|improve this answer


























                  3












                  3








                  3







                  problems



                  there are a number of problems with your code:





                  1. Access should be access

                    in java it is convention to type a lower case letter at the start of a variable and an upper case letter at the start of a class name, that way classes en variables are easily distinguishable


                  but mainly





                  1. list doesn't exist so you should make a variable.


                  solutions



                  arrays



                  public AccessLevel getAccess() {
                  String accessesString = "MERIA : SMI";
                  String split = accessesString.split(" : ");

                  AccessLevel list = new AccessLevel[split.length];

                  for (int i = 0; i < split.length; i++) {
                  list[i] = AccessLevel.valueOf(split[i]);
                  }
                  return list;
                  }


                  List



                  you should really use List instead of basic arrays, they're more versatile and easier to use.



                  public List<AccessLevel> getAccess() {
                  String accessesString = "MERIA : SMI";
                  String split = accessesString.split(" : ");

                  List<AccessLevel> list = new ArrayList<>();

                  for (String access : split) {
                  list.add(AccessLevel.valueOf(access));
                  }
                  return list;
                  }


                  Streams



                  The most elegant solutions in my opinion but maybe a bit more difficult.



                  public List<AccessLevel> getAccess() {
                  String accessesString = "MERIA : SMI";
                  String split = accessesString.split(" : ");

                  return Arrays.stream(split)
                  .map(AccessLevel::valueOf)
                  .collect(Collectors.toList());
                  }


                  I hope that was helpful!
                  greetz






                  share|improve this answer













                  problems



                  there are a number of problems with your code:





                  1. Access should be access

                    in java it is convention to type a lower case letter at the start of a variable and an upper case letter at the start of a class name, that way classes en variables are easily distinguishable


                  but mainly





                  1. list doesn't exist so you should make a variable.


                  solutions



                  arrays



                  public AccessLevel getAccess() {
                  String accessesString = "MERIA : SMI";
                  String split = accessesString.split(" : ");

                  AccessLevel list = new AccessLevel[split.length];

                  for (int i = 0; i < split.length; i++) {
                  list[i] = AccessLevel.valueOf(split[i]);
                  }
                  return list;
                  }


                  List



                  you should really use List instead of basic arrays, they're more versatile and easier to use.



                  public List<AccessLevel> getAccess() {
                  String accessesString = "MERIA : SMI";
                  String split = accessesString.split(" : ");

                  List<AccessLevel> list = new ArrayList<>();

                  for (String access : split) {
                  list.add(AccessLevel.valueOf(access));
                  }
                  return list;
                  }


                  Streams



                  The most elegant solutions in my opinion but maybe a bit more difficult.



                  public List<AccessLevel> getAccess() {
                  String accessesString = "MERIA : SMI";
                  String split = accessesString.split(" : ");

                  return Arrays.stream(split)
                  .map(AccessLevel::valueOf)
                  .collect(Collectors.toList());
                  }


                  I hope that was helpful!
                  greetz







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 3 at 2:53









                  Louisb BoucquetLouisb Boucquet

                  514




                  514






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015508%2fnew-enum-with-array%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith