Java mismatch between String and String[] [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm using Java to implement the Apriori algorithm but one problem exists.
ArrayList<String> l1=new ArrayList<>();//L1
...
ArrayList<String[]> lk1=l1;//Lk-1
Then it warns that: cannot convert from ArrayList<String> to ArrayList<String[]>. How can I solve this problem?

They're different types. Ignore the collection and just look at the underlying type.
String str = "This is a string.";
String[] array = str;
These are two different types, unconvertable, so as T for the collection, the collections are then unconvertable. Very basic concept here.

Related

How to convert a String to ArrayList? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I store an ArrayList in a file as a String ( arrayList.toString()).
How do i convert it back into an ArrayList?
Overall, I just want to convert something like: [Eve, Anna, Tony, Steve] back to an ArrayList. Any ideas?
All you need is a java.lang.String's split() & java.util.Arrays asList():
String fruits = "Orange,Apple,Grape";
String[] array = fruits.split(",");
List<String> list = Arrays.asList(array);
//Code to check the content in the List (for your understanding only)
for(String s : list) {
System.out.println(s);
}
Prints below output:
Orange
Apple
Grape

How to judge isInstance(ArrayList<>) in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How to judge whether a variable is an ArrayList in Java? Like the isInstance function.
Just use instanceof, blow is very simple example how to detect a variable is ArrayList:
List<String> strings = new ArrayList<>();
if (strings instanceof ArrayList) {
System.out.println("ArrayList");
}
a.getClass().getName() - will give you the type of the value of the variable, but not the variable's type.
boolean b = a instanceof String - will give you whether or not it is an instance of a specificclass.
I took this information from: How know a variable type in java?
This can happen. I'm trying to parse a String into an int and I'd like to know if my Integer.parseInt(s.substring(a, b) is kicking out an int or garbage before I try to sum it up.

How to shuffle a char array - JAVA? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am not sure how to shuffle a char array using Java so could anyone
help me, but please keep it really simple because I'm at GCSE level right now, Thank you.
Try using Collections.shuffle method like:
Character[] myarray = ...
List<Character> charList = Arrays.asList(myarray);
Collections.shuffle(charList);
Character[] myShuffledArray = (Character[]) charList.toArray();

java string add comma become array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
String items = "bookcupdoll";
I want to add a comma in items variable like below:
book,cup,doll
How should I make the string become an array?
If you are asking how to convert a string to an array...
There is a handy method called "split(String separator)"
If the string looks like : String items = "book,cup,doll";
you can use it like this
String[] array = items.split(",");
And you will end up with an array of size 3 with the 3 different elements.

Java programming Input:Today is Sunday Output:Sunday is today [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
String str="Today is Sunday"
And the output should be "Sunday is Today"
I need a code for this program in Java.
Is there any other method to do it...
This feels like a question for a university assignment, but in essence, you need to split the array then reverse it.
var elems = inStr.split(' ');
elems.reverse();
Unless I'm misunderstanding the question.
For the record, JavaScript and Java are different languages, and I've written JavaScript.
EDIT: In Java, it's something like this:
String inStr = "today is thursday";
String[] elems = inStr.split("\ ");
You can then just loop backwards over the array.
In javascript, you can use push() method to store value into array, It adds the new item into end of the array.
var arr = [];
arr.push('Today')
arr.push('is')
arr.push('sunday')

Categories

Resources