0

I have two classes namely Test and Demo and in Demo class I have a class variable namely List. I somehow need to put/add the given two strings into List but it will only accept objects of Test class. Is there a way I can add the given strings to List.

class Test{

List<Test> getdataFromDemo(){
   return Demo.getData;
  }
}

class Demo{

public static List<Test> getData=new LinkedList<Test>;

static void D1(){
String str1="TestOne";
String str2="TestTwo";
}

getData.add(str1);//need to add str to getData but this is showing error
getData.add(str2);
System.out.println(getData)
4
  • 1
    Well, that's what List<Test> means, you can only put Test objects in that list. Why do you want to put strings in there? Commented Jun 3, 2020 at 18:41
  • Define your list as List<String> Commented Jun 3, 2020 at 18:41
  • I have data that is coming up from source in the form of Strings and then this particular class is defined as List<Test> which I cant change as well Commented Jun 3, 2020 at 18:42
  • 2
    Between this comment and the one to the answer below, you're basically stuck. You literally can't put a string in that list, and if you can't make changes then you're at a dead stop. Go back to whoever is specifying these requirements and ask them for a solution. Commented Jun 3, 2020 at 18:48

2 Answers 2

1

You are trying to store a String in your generic list of Test. You need to create an attribute in Test of the type String and set it within the constructor. Then you need to add the new objects of Test with the Strings str1 and str2 in the constructor to the list.

public class Test {

    public String name;

    Test(String name) {
        this.name = name;
    }

    List < Test > getdataFromDemo() {
        return Demo.getData;
    }
}

class Demo {

    public static List < Test > getData = new LinkedList < Test > ();

    public static void main(String[] args) {
        String str1 = "TestOne";
        String str2 = "TestTwo";
        getData.add(new Test(str1)); // need to add str to getData but this is showing error
        getData.add(new Test(str2));
        System.out.println(getData);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Add a field variable in test class of string and initialize its in constructor. or just set the string directly using setters and fetch it using getters.

class Test{
public String testString;
public void setTestString() {
  this.testString=testString;
}

public String getTestString() {
  return this.testString;
}

List<Test> getdataFromDemo(){
   return Demo.getData;
  }
}

class Demo{

public static List<Test> getData=new LinkedList<Test>;

static void D1(){
String str1="TestOne";
String str2="TestTwo";
}
Test test1 =  new Test()
test1.setTestString( str1);
getData.add( test1);//need to add str to getData but this is showing error
Test test2 =  new Test()
test2.setTestString(  str2);
getData.add(test2);
System.out.println(getData)

1 Comment

no it has to be empty constructor as i m using it it in multiple places , if i will initialize it then it would create a problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.