0

I have a java program called "run.java" which calls another program called "Main.java".

Main.java takes as an argument a LinkedList. So i should pass this LinkedList from run.java to Main.java

so, in Main.java, how can I cast the argument from string to LinkedList ?

2
  • 4
    this does not make any sense without providing some code. Commented Mar 3, 2011 at 9:50
  • 2
    You can't cast a String to a LinkedList :) IMO you have misunderstood something, I'd recommend telling us what you want to do (ex: I want to make a program that stores 10 strings in a LinkedList) or posting some example code so we can understand you better. Commented Mar 3, 2011 at 9:53

2 Answers 2

6

If you want to transform an array of strings (String[] args) into a linked list, then use this code :

LinkedList<String> argsAsLinkedList = new LinkedList<String>(Arrays.asList(args));

But a method should almost never take a LinkedList as argument. It should take a List. Use interfaces rather than concrete classes for your arguments : it allows your method to work with other kinds of lists.

Sign up to request clarification or add additional context in comments.

Comments

0

You will have to tokenize your String, have a look at the StringTokenizer class.

Comments

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.