Why This Extension is not Compiling?

Thanks, @vknow360 and @Alpha2020 for more resources for learning java. I recommend w3.schools if you are a total newb to java (like me :wink:). Start with that and then do the Udemy. I also recommend Javascript for dummies, and Java for beginners, and Java for Android Devices for dummies. They all average at about 20-30 USD.

1 Like

Please explain this piece of code more @AkshatRana. Like @Kumaraswamy said,

none of us can read your mind. (well, unless you mod life, which IIR, you can't).

In java listName[index] works for me but using in the appybuilder it is not compiling why?

I was learning some arays parts today and tried to implement it on Appybuilder IDE but it failed and thats why i asked here..,. I am very new to Java. Thanks for your suggestion.

That's because the ArrayList isn't a java array. You might want to use the yailList.toArray() ( to convert the YailList to Object array ) or the yailList.toStringArray() ( to convert the YailList to String array ),
For example:

String[] stringArray = list.toStringArray();
String val = stringArray[index];

Note: java index starts from 0, and not 1 as in Appinventor, you might want to subtract one from the index given to have the right index starting from 0.

3 Likes

Thanks for responding m.Okay ... i have to change yaillist array to object array/String arary and after that it will work for me...

Again a question, I ask for the list from user using yailList then
I will convert it into String array but what if i have to return edited Array... I again have to convert it into Yaillist naa??

For eg;
I ask for Arrays and user gave me ["Array1","Array2",Array3","Array4"]
then i converted it into String array first and using Slice Method I deleted lets "Array 3" and I have to return it into Yailist or not ? If yes then how??

That depends on whether you want to manipulate the list in place or return a copy.

Returning a copy is definitely the easier option. You would call YailList.makeList to create a new list from your modified collection.

If you want to modify the list in place, there's a hack you could do where you could make a new copy and then call original.setCdr(copy.getCdr()). The more "correct" approach would be to iterate over the list and splice out the pair you're interested in removing.

Edit: It's important to note that YailList internally is implemented as a linked list structure, not as an array. Furthermore, the first element in the list is always the Scheme symbol *list*, which makes the 1-based indexing work. These two facts together are what makes the shortcut in the previous paragraph work because you're modifying the first pair, (*list* ...) by setting its second pointer to the same as your copied list.

It willl be definately easier option for ,me. Thanks @ewpatton for responding .

Even converting it into an array the first list element will always be (*list.) that's why we will start index from 1 .. Am I right??

You can return List and YailList both.
But if you want to return List then you will have to first convert YailList into List.

Yes, AI2 starts index from 1.

1 Like

Thanks to you @vknow360

When you use the toArray or toStringArray methods the *list* element is dropped. This allows you to use 0-based indexing in Java like one would expect.

When returning a value, if you aren't planning to do sufficient type checking it is usually better to return a List<...> rather than a YailList. If you return List, the runtime will check it automatically to ensure that its contents are acceptable for the blocks language. If you return YailList, it is assumed you've taken the effort to check. This can lead to problems if you haven't sanitized the list data for the user.

4 Likes

I am trying to explain that use void instead of Yailist in

and you can take reference that @ewpatton said

What does that even do? You're not taking input whereas you're also not giving output...?

I had mistakenly left input..

In the case of output, you can use @SimpleEvent

Keep it simple as you can :grinning:

1 Like
@SimpleEvent(description = "")
public void GotItem(String item){
  EventDispatcher.dispatchEvent(this, "GotItem", item);
}

@SimpleFunction(description = "")
public void SelectItem(YailList list, int index){
  String item = "";
  item = (String) list.get(index);
  GotItem(item);
}

@AkshatRana is saying about this code... :slightly_smiling_face:

Yess... I was talking about this @Alpha2020 :slightly_smiling_face:

1 Like

The code is wrong. You should use get(INDEX) method for YailList.

See here-


https://www.w3schools.com/java/java_arrays.asp

1 Like