0

I am writing a program involving ArrayList. The program is about editing the ArrayList through user input:

ArrayList<Type> array = new ArrayList<Type>();

switch (verify)
{

case 1:
  //A list of statement including variables, conditionals and loops
  break;

case 2:
  //Another list of statement here includes variables, loops etc
  break;

default:
  do something else;

}

My question is that I don't want to put a lot of statement in a single case, is it possible for me to separate them and redirect the Java to read blocks of statement from somewhere else?

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 10
    I guess you call those blocks of code that redirect somewhere else "method" :) – zapl Dec 01 '13 at 23:01
  • You can avoid using break after the case has ended - then the switch case will fall-through - so to speak. For instance leaving out the break in case:1, will then execute both case 1 and case 2 – Andersnk Dec 01 '13 at 23:06

2 Answers2

4

Use methods and pass the ArrayList to them:

public static void main(String args) {
  ArrayList<Type> array = new ArrayList<Type>();

  switch (verify) {

    case 1:
      case1(array);
      break;

    case 2:
      case2(array);
      break;

    default:
      do something else;
  }
}

private static void case1(ArrayList<Type> array) {
  //A list of statement including variables, conditionals and loops
}

private static void case2(ArrayList<Type> array) {
  //Another list of statement here includes variables, loops etc
}

The ArrayList is a mutable type, so any changes you make to the list within the case1() and case2() methods affect the ArrayList in your main method.

rjsang
  • 1,757
  • 3
  • 16
  • 26
  • Thank you! It works! ^_^ Just a question, one the "sub Method" is it: "private static void" instead of "private void"? :3 – user2789240 Dec 01 '13 at 23:35
  • 1
    If you are happy that this answer solves your problem, please click to "accept" it. The most basic answer to your second question is that your "main" method is static, so it can't access the others unless they are also static. For a more detailed explanation, check out this excellent question and answer: http://stackoverflow.com/questions/11993077/difference-between-static-methods-and-instance-methods – rjsang Dec 01 '13 at 23:46
  • ah~ okey~ I clicked it and thanks for the help ^_^ – user2789240 Dec 02 '13 at 00:05
0

Use functions!

public class Main {
  public static void main(String[] args) {
    //snip
    ArrayList<Integer> array = new ArrayList<Integer>();
    switch(verify) {
      case 1: doAThing(array);
        break;
      case 2: doSomethingElse(array);
        break;
      default: doAThirdThing(array);
    }
  }

  private static void doAThing(ArrayList<Integer> array) {
    //do logic here
  }

  private static void doSomethingElse(ArrayList<Integer> array) {
    //do logic here
  }

  private static void doAThirdThing(ArrayList<Integer> array) {
    //do logic here
  }

By the way, I'm impressed you're already looking for ways to keep your code clean and easy to write. Mature programmers usually break out complicated code into smaller functions, exactly like you're thinking about doing. I'm excited for you to continue studying Java!

Kevin
  • 14,655
  • 24
  • 74
  • 124
  • 1
    Thanks for the encouragement XDDD Yes, my professor is telling me that keep the code clean is very important and I waana be a programmer :3 – user2789240 Dec 02 '13 at 00:05