How to send ArrayList send an other class [duplicate] - java

This question already has answers here:
accessing a variable from another class
(7 answers)
Closed 6 years ago.
How can I send an ArrayList from one class to another in java?
I want to send a variable in a form of a object from one class to another. I tried several ways such as final static but I failed.
Do you have any solution?
public class A(){
private int a1,a2;
public geta1(int a1);
public geta2(int a2);
public seta1(int a1);
public seta2(int a2);}
public class class1(){
A =new A();
A.seta1(5);
//....................}
public class class2(){
//}
How I can build an object in class one and a1=5 in a way that this object could be available in class 2 too with this amount.

You could store the ArrayList in class1 and create a getter method that returns it.
e.g.
import java.util.ArrayList;
public class class1 {
private ArrayList<Integer> myList;
public ArrayList<Integer> getList() {
return myList;
}
//..other methods of the class
}
And then:
class1 object = new class1();
ArrayList<Integer> myList = object.getList();
You could also make it static (part of the class, rather than a part of an instance of the class) if that's what you need
import java.util.ArrayList;
public class class1 {
private static ArrayList<Integer> myList;
public static ArrayList<Integer> getList() {
return myList;
}
//..other methods of the class
}
and then just call
ArrayList<Integer> myList = class1.getList();

Related

How to add object having 4 attributes in array in java [duplicate]

This question already has answers here:
How to create a Java class with data fields
(2 answers)
Closed 2 years ago.
Object[] Flights = new Object[10];
This is an Object array, now what i want is to add 4 attributes to each object which includes String and integer?
How can I do this ?
You need to create a custom class and define the attribute that you want
public class Solution {
static class CustomObj {
int id;
String name;
// your properties
}
public static void main(String[] args) {
CustomObj[] objsC = new CustomObj[10];
// your logic
}
}
I believe you can't do this in Java, you absolutely need to create a class.
public class Myclass {
public String myString;
public int myInteger;
}
and create an instance of that class :
public mainClass {
public void main(String args[]){
MyClass myInstance = new MyClass();
myInstance.myString = "the value you want";
myInstance.myInteger = 1;
}
}

Interface method is not visible in other class [duplicate]

This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 4 years ago.
I have this code:
public interface Interface1{
void interfaceMethod1();
}
public class Class1<T extends Class0&Interface1>{
private T field;
public T getField(){
return field;
}
}
When I invoke class1.getField().interfaceMethod1(), where class1 is Class1 instance, I see error "Cannot resolve method".
I want to define class with generic field which will include methods from Class0 and Interface1.
Assume we have next definitions:
public interface Interface1 {
void interfaceMethod1();
}
public class Class0 {
}
public class Class2 extends Class0 implements Interface1 {
#Override
public void interfaceMethod1() {
}
}
public class Class1<T extends Class0 & Interface1> {
private T field;
public T getField() {
return field;
}
}
When you have
Class1 class1 = new Class1();
class1.getField().interfaceMethod1();
it is called type erasure. It means all generic arguments assumed to be Object, so class1.getField() return Object which lack of interfaceMethod1 method.
To fix that you should do this:
Class1<Class2> class1 = new Class1<>();
class1.getField().interfaceMethod1();
Now everything compiles fine.

How can I transfer ArrayLists between classes?

I am a rather large-scale noob. I have come across the answer a multitude of times, but, it was just not in a format I could understand. Right now, I am attempting to send a List full of values, to a separate class. I tried each of the resources but to no avail. Apologies if this is an inappropriate question here, a simple ask-with-no-code.
You need a method or a constructor on the class the receives the ArrayList that accepts ArrayLists.
Pseudocode:
public class Class1 {
//Constructor
public Class1(){}
//Methods
public ArrayList createArray(){
//Your code here
}
}
public class Class2 {
//Constructor
public Class2(ArrayList myArray){
//Your code here
}
}
public class Test{
Class1 c1 = new Class1;
ArrayList array = c1.createArray();
Class2 c2 = new Class2(array);
}
class list_receiver{ //class in which you want to send your list
public void print_array_list(ArrayList<String> l ){
System.out.println(l);
}
}
class list_sender{//class from where you are sending your list
public static void main(String args[]){
list_receiver r = new list_receiver();
ArrayList<String> list=new ArrayList<String>();
list.add("A");
list.add("B");
r.print_array_list(list);
}
}

Storing values from an abstract int on start-up

so I have an abstract class and i'm willing to store all the values from the sub-classes in an ImmutableList. Here is an example on what I mean
public abstract class Test {
...
public abstract int getValue();
}
then the sub-class
public final class Example extends Test {
#Override
public int getValue() {
return 5;
}
}
Is there a way to store the Test#getValue() in an ImmutableList on start-up?
I tried doing something like
public abstract class Test {
public static final ImmutableList<Integer> VALUES = ImmutableList.of();
public Test() {
VALUES.add(getValue());
}
public abstract int getValue();
}
then print out the values in the VALUES list.
public static void main(String[] args) {
Test.LIST.forEach(System.out::println);
}
but it didnt work.
use an initializer block. It's possible to create a static block which will execute upon class load:
package foo.bar.baz;
import java.util.*;
public class Test {
static {
int MY_INT = 5;
List<Object> mylist = new ArrayList<Object>();
mylist.add(new Integer(MY_INT));
}
public Test() {
// ...
}
}
You can write in the main method like this :
Reflections reflections = new Reflections("com.TestClassExample");
Set<Class<? extends >> classes = reflections.getSubTypesOf(TestExampleClass.class);
Get all the names of the classes and then loop through all the classes, and then cast it in the your test class and , then using and storing the values dynamically in a variable like this.
private static List<Integer> immutableList = new ArrayList<Integer>();
Does this sound feasible for your problem ?

How can I create object of class whose constructor is defined as private [duplicate]

This question already has answers here:
How to create objects from a class with private constructor?
(6 answers)
Closed 7 years ago.
I want to create object of Base class in another class but Base class constructor is defined as private
Here is my Code
public class Main
{
public static void main(String ... args)
{
//Base objBase = new Base();
//objBase.show();
}
}
class Base
{
private Base()
{
}
public void show()
{
System.out.println("Base Class Show() Method");
}
}
Code inside Base is still allowed to call the constructor, which means it's possible to create objects in a static method:
class Base
{
private Base()
{
}
public void show()
{
System.out.println("Base Class Show() Method");
}
public static Base createBase() {
return new Base();
}
}
and then call the method to create an object:
Base objBase = Base.createBase();
objBase.show();
You cannot create objects of classes if they have private constructors. Objects can be constructed, but only internally. That is how it is.
There are some common cases where a private constructor can be useful:
Singletons
Classes containing only static methods
Classes containing only constants
Type safe enumerations
Hoping this helps.

Categories