All combinations of ArrayList<ArrayList<String>> Lists in JAVA [closed] - java

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 6 years ago.
Improve this question
I need help with a implementation of a function that gives me all combinations of ArrayList<ArrayList<String>> (not List<List<String>>) List in fix order.
An Example for what I need:
From:
List1:[1,2]
List2:[3,4]
List3:[5,6]
To:
String 1 "1,3,5"
String 2 "1,3,6"
String 3 "1,4,5"
String 4 "1,4,6"
String 5 "2,3,5"
String 6 "2,3,6"
String 7 "2,4,5"
String 8 "2,4,6"
All Strings in ArrayList<String> Object
Please help me, I have no idea how to implement it.
PS: The size of ArrayList<ArrayList<String>> is variable.
PSS: ...and the size from the ArrayList lists are variable.

After little changes from the old rev it works now (ArrayIndexOutOfBoundsException and deleting to much on result):
void generate(ArrayList<ArrayList<String>> lists,
ArrayList<String> result, int index) {
if (index >= lists.size()) {
System.out.println(String.valueOf(result));
return;
}
ArrayList<String> list = lists.get(index);
for (int i = 0; i < list.size(); i++) {
result.add(list.get(i));
generate(lists, result, index + 1);
result.remove(result.size() - 1);
}
}
}

Related

How to find the Index of string in java array from the given string value [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 9 months ago.
Improve this question
creditTIDstatusArray=[93312263-1-09722612223, 99802001-1-09102842369, 99802002-1-09102842369];
creditTIDstatusList.addAll(Arrays.asList(creditTIDstatusArry));
searchValue="99802002-1".
int retval=creditTIDstatusList.indexOf("99802002-1");
System.out.println("The element at index is:" retval);
Output: 2
Please let me know how I can find the index of the given above(searchValue)element.
As you never have posted the reproducible code.
Assumptions
creditTIDstatusArray is a String type array.
Your search query always stays in front of each String value in the array.
Multiple indexes may start with the same search value.
String[] creditTIDstatusArray=new String[]{"93312263-1-09722612223", "99802001-1-09102842369", "99802002-1-09102842369"};
String searchValue="99802002-1";
for (int i = 0; i < creditTIDstatusArray.length; i++) {
if(creditTIDstatusArray[i].startsWith(searchValue)){
System.out.println("Index :" + I); // this will print all the indexes that starts with searchvalue
}
}
I am not sure why you have added the array into a list and then searched index of because it will never work as you are searching only part of a String rather than the whole value or object.

Refactoring a for loop into a stream [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 2 years ago.
Improve this question
How to change this for loop into stream?
public int calculateForSpliterator(String[] matchTab, String spliterator, RulesChecker rulesChecker) {
int finalScore = 0;
for (String element : matchTab) {
String[] splitScroes = element.split(spliterator);
int ourPoints = Integer.parseInt(splitScroes[0]);
int theirPoints = Integer.parseInt(splitScroes[1]);
finalScore += rulesChecker.checkRules(ourPoints, theirPoints);
}
return finalScore;
}
Assuming RulesChecker#checkRules returns an int, then you can use:
public int calculateForSpliterator(String[] matchTab, String spliterator, RulesChecker rulesChecker) {
return Arrays.stream(matchTab)
.map(element -> element.split(spliterator))
.mapToInt(splitScores -> rulesChecker.checkRules(Integer.parseInt(splitScores[0]),
Integer.parseInt(splitScores[1])))
.sum();
}
Notice how this isn't much more readable than your current solution, so I probably would keep the for-loop if I were you.

How tho check in Java if there is same characters in a String? [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 5 years ago.
Improve this question
I've got a 6 char long string, and I would like to check, if there is just one from each character. How to?
You could compare the number of distinct characters to the length of the string:
boolean charMoreThanOnce = s.chars().distinct().count() < s.length();
You can do it using a Set. You need unique elements and Set gurantees you containing the unique elements. HashSet is implementation of Set, you can use it to implement this idea.
public boolean ifAllCharsUnique(String input){
char[] arr = input.toCharArray();
int length = arr.length;
Set<Character> checker = new HashSet<>();
for(int i =0 ; i < length; i++){
if(checker.contains(arr[i]){
return false;
}
checker.add(arr[i]);
}
return true;
}

How to convert ArrayList of Strings to ints, so they can be added? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to convert each String in an ArrayList to int so then I can add them,
asuma is an ArrayList containing String
I need to iterate over this ArrayList to convert String to int
for (int i = 0; i <=asuma.size(); i++) {
and then I need to add the integers. How do I do it?
I suspect that the ArrayList you are referring to is asuma and that it is an ArrayList<String>. In that case, you can do this:
for (String s : asuma) {
int valorFinal = Integer.parseInt(s);
}
I think u try that:
for (int i = 0; i <=asuma.size(); i++) {
int valorFinal = Integer.parseInt(asuma.get(i));
}

Comparing multiple strings 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 8 years ago.
Improve this question
I have 5 distinct strings called, say, string1 through string5.
I want to write a simple if statement that runs if any two of the five strings contain the same string. How would I do that?
Thanks in advance!
Comparison is a binary operation, therefore you can always compare only two objects at a time. I would suggest using a cycle and comparing each string to the remaining ones.
public boolean multipleStringEquals(String[] strings) {
for (int i = 0; i < strings.length; i++) {
for (int j = i + 1; j < strings.length; j++) {
if (strings[i].equals(strings[j])) {
return true;
}
}
}
return false;
}

Categories