User avatar
Zilla707
Posts: 83
Joined: Fri Aug 23, 2019 11:04 pm

Method in ArrayList?

Fri Sep 27, 2019 9:53 pm

Hello, I have done some java, but I mostly do python3, and in python3 you can add a function to a list:

Code: Select all

>>> def test():
	print('Test')
	
>>> testList = []
>>> testList.append(test)
>>> testList[0]()
Test
So my question is: is there a way to do this, (or something like it), in java?
Aim for perfect and you'll hit somewhere near pretty good. (maybe...)
A quick wit is best followed by quick reflexes. (and a Band-Aid...)

User avatar
clicky
Posts: 418
Joined: Thu Oct 25, 2012 7:34 am

Re: Method in ArrayList?

Sat Sep 28, 2019 10:29 am

Java is very versatile object oriented language that can do many things and falls in very similar category as Python.

So, answer is: yes.

All you have to do now is to learn it. Internet is full of resources, examples and sites where you can even learn it online, do online courses and such.

knute
Posts: 487
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: Method in ArrayList?

Sat Sep 28, 2019 11:15 pm

It's a little more complicated in Java.

Code: Select all

import java.util.*;
import java.util.function.*;

public class SSCCE {  // simple self contained compilable example
    public static void main(String... args) {
        // returns the argument unchanged
        UnaryOperator<String> u1 = UnaryOperator.identity();
        // returns the argument changed to lower case
        UnaryOperator<String> u2 = String::toLowerCase;
        // returns the argument's characters reversed
        UnaryOperator<String> u3 = f -> {
            return new StringBuffer(f).reverse().toString();
        };
        // returns the argument's characters reversed and then to lower case
        Function<String,String> u4 = u3.andThen(u2);  // or .compose

        // creates a list to hold functions
        List<Function<String,String>> list = new ArrayList<>();

        // add the functions to the list
        list.add(u1);
        list.add(u2);
        list.add(u3);
        list.add(u4);

        // apply the functions to the arguments
        System.out.print(list.get(0).apply("Hello"));
        System.out.println(list.get(1).apply(" WORLD!"));
        System.out.println(list.get(2).apply("SRETTEL EHT ESREVER"));
        System.out.println(list.get(3).apply(
         "ESAC REWOL OT SRETTEL EHT ESREVER"));
    }
}
C:\Users\Knute>java SSCCE.java
Hello world!
REVERSE THE LETTERS
reverse the letters to lower case

User avatar
Zilla707
Posts: 83
Joined: Fri Aug 23, 2019 11:04 pm

Re: Method in ArrayList?

Sun Sep 29, 2019 10:31 pm

Thanks, I still don't know very much about java, (at least in relation to python), but that's very helpful.
Aim for perfect and you'll hit somewhere near pretty good. (maybe...)
A quick wit is best followed by quick reflexes. (and a Band-Aid...)

DamienIvy
Posts: 1
Joined: Sat Nov 30, 2019 12:22 pm

Re: Method in ArrayList?

Sat Nov 30, 2019 12:39 pm

What I can understand you want to append a value to the List using python code.

In Java , you can append the value to the ArrayList using add() method of ArrayList class.

Code: Select all

ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add("test");
System.out.println(arrlist);
 
Reference:
https://javahungry.blogspot.com/2017/10 ... ample.html

Return to “Java”