Page 1 of 1

Method in ArrayList?

Posted: Fri Sep 27, 2019 9:53 pm
by Zilla707
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?

Re: Method in ArrayList?

Posted: Sat Sep 28, 2019 10:29 am
by clicky
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.

Re: Method in ArrayList?

Posted: Sat Sep 28, 2019 11:15 pm
by knute
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

Re: Method in ArrayList?

Posted: Sun Sep 29, 2019 10:31 pm
by Zilla707
Thanks, I still don't know very much about java, (at least in relation to python), but that's very helpful.

Re: Method in ArrayList?

Posted: Sat Nov 30, 2019 12:39 pm
by DamienIvy
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

Re: Method in ArrayList?

Posted: Mon Mar 23, 2020 7:50 am
by aryank4564
This is a pretty clear example of how to add data to ArrayList in python. However, if you want to learn how to append data into the ArrayList in Java. Just see the below code

Code: Select all

ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Object 1");
arrayList.add("Object 2");
System.out.println(arrayList); 


See Reference : https://www.java8net.com/2020/02/arraylist-in-java.html

Re: Method in ArrayList?

Posted: Wed Feb 17, 2021 9:27 am
by anmoldhiman
You can use add() method of ArrayList

' List<String> list = new ArrayList<String>();
list.add("shivam");

You can visit ArrayList examples
https://www.javavogue.com/2015/05/java- ... ples.html/

https://www.javavogue.com/2015/05/how-t ... java.html/