How to pick up array index?

Asked

Viewed 1,446 times

-2

I have the following APEX code

List<integer> entrada = new List<integer>();

public static void  generateStringArray(integer entrada){
 List<String> ListaPrincipal = new List<String>();


    for(integer i = 0; i < entrada; i++){

       System.debug(entrada.get(i).toString);


    }



}

I’m trying to get the index i. When I execute him, he makes the following mistake:

Method does not exist or incorrect Signature: void get(Integer) from the type Integer

  • With input[i] instead of get(i), it works?

  • You have several errors in the code. It would also be interesting to say what this is integer ? Or would I rather have Integer ? The best thing would be to put the code just like yours in your file, but if it’s the way it is here, then it’s still a long way from compiling.

2 answers

3

Your code is riddled with errors:

  1. Java is case sensitive. Class name is Integer, and not integer.

  2. Cannot instantiate interfaces directly. So, use new List is not allowed. You must instantiate an implementation such as ArrayList.

  3. The method System.debug does not exist. Maybe what you wanted was to use System.out.println.

  4. Note that you have two different things called entrada. One is the list and the other is the method parameter generateStringArray. Change the name of one of them to avoid confusion.

  5. Note that the ListaPrincipal is not used anywhere after created. Therefore, it is unnecessary.

  6. Since toString is a method, you should not forget to put the () in it.

  7. Do not use Integer where int is enough. A int, for being a primitive type, can never be null. Already the Integer, because it is an object, it can be null.

I think your corrected code might look like this:

List<Integer> lista = new List<Integer>();

public static void generateStringArray(int entrada) {
    for (int i = 0; i < entrada; i++) {
       System.out.println(lista.get(i).toString());
    }
}

3

You are trying to get the index of a variable called entrada which is a integer (I imagine this is also wrong, may want to use Integer or int), but actually what you probably want is to take the variable ListaPrincipal which is a list and how every list object has the method get().

There are other errors in the code. This makes more sense:

List<Integer> lista = new List<Integer>();

public static void generateStringArray(int entrada) {
    if (entrada < lista.length()) return;
    for (int i = 0; i < entrada; i++) {
       System.out.println(lista.get(i));
    }
}

I put in the Github for future reference.

Ideally it should have a return indicating whether it worked or not, but it depends on the requirement.

  • I refactored the question and the code

  • No, you deleted the original question and asked a new question and you can’t do that, you have to leave the original and if you have new problem you ask a new question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.