Printing empty brackets? - java

I have a simple program that should take two arrays, convert them into lists, and take out the values that are the same. For some reason though I am getting a value of:
[]
[]
[]
[]
Although the result should be "eggs", "lasers", "hats", "pie" - "lasers", "hats" which should get me a result of "eggs" "pie".
Here is my code:
public class Arraystring {
public static void main(String[] args) {
String[] things = { "eggs", "lasers", "hats", "pie" };
List<String> list1 = new ArrayList<String>();
for (String x : things) {
list1.add(x);
}
String[] thingstwo = { "lasers", "hats" };
List<String> list2 = new ArrayList<String>();
for (int i = 0; i < list2.size(); i++) {
list2.add(thingstwo[i]);
}
editlist(list1, list2);
}
public static void editlist(Collection<String> l1, Collection<String> l2) {
Iterator<String> it = l1.iterator();
while (it.hasNext()) {
if (l2.contains(it.next())) {
System.out.println("hui");
it.remove();
}
System.out.println(l2);
}
}
}

First your line in Arraystring {..}
for (int i = 0; i < list2.size(); i++) {
should be
for (int i = 0; i < thingstwo.length; i++) {
And then you are printing the wrong list in editlist(..):
System.out.println(l2);
should be
System.out.println(l1);

Related

Moving data to different methods

I am trying to figure out how to save a data in a submethod several times.
For example the code below is creating and array of strings then the array is moved to an arraylist five times. How to make the program save all strings 5 times. With other words, if I print out the array list newList .How to get the following output?
word0, word1, word2, word3, word4, word0, word1, word2, word3, word4, word0, word1, word2, word3, word4, word0, word1, word2, word3, word4, word0, word1, word2, word3, word4.
public static void main(String[] args) {
String[] list = new String[5];
for (int i = 0; i < list.length; i++) {
list[i] = "word" + i;
}
for (int i = 0; i < 5; i++) {
experiment(list);
}
}
public static void experiment(String[] list) {
ArrayList<String> arrList = new ArrayList<>();
for (int i = 0; i < list.length; i++) {
arrList.add(list[i]);
}
saveItAll(arrList);
}
public static ArrayList<String> saveItAll(ArrayList<String> counter) {
ArrayList<String> newList = new ArrayList<>();
newList = counter;
System.out.println(newList);
return newList;
}
You need to store it outside of the method and statically.
public class Test
{
static List<String> newList = new ArrayList<>();
public static void main(String[] args)
{
String[] list = new String[5];
for (int i = 0; i < list.length; i++) {
list[i] = "word" + i;
}
for (int i = 0; i < 5; i++) {
experiment(list);
}
System.out.println(newList);
}
public static void experiment(String[] list)
{
List<String> arrList = new ArrayList<>();
for (int i = 0; i < list.length; i++) {
arrList.add(list[i]);
}
saveItAll(arrList);
}
public static void saveItAll(List<String> counter)
{
newList.addAll(counter);
}
}
Another word here: You would not need the saveItAll method, since addAll is also doing the job here. Then you usually don't use Lists with there implementing type, you usually use the interface to define the type of it so the implementation would be switchable. That's what interfaces are for.

Java array I need your thought

Given an array of strings, return another array containing all of its longest strings.
For (String [] x = {"serm", "aa", "sazi", "vcd", "aba","kart"};)
output will be
{"serm", "sazi" , "kart"}.
The following code is wrong, What can I do to fix it.
public class Tester {
public static void main(String[] args) {
Tester all = new Tester();
String [] x = {"serm", "aa", "sazi", "vcd", "aba","kart"};
String [] y = all.allLongestStrings(x);
System.out.println(y);
}
String[] allLongestStrings(String[] input) {
ArrayList<String> answer = new ArrayList<String>(
Arrays.asList(input[0]));
for (int i = 1; i < input.length; i++) {
if (input[i].length() == answer.get(0).length()) {
answer.add(input[i]);
}
if (input[i].length() > answer.get(0).length()) {
answer.add(input[i]);
}
}
return answer.toArray(new String[0]);
}
}
I will give you solution, but as it homework, it will be only sudo code
problem with your solution is, you are not finging longest strings, but strings same size or bigger than size of first element
let helper = []
let maxLength = 0;
for each string in array
if (len(string) >maxLength){
maxLength = len(string);
clear(helper)
}
if (len(string) == maxLength)
helper.add(string)
}
return helper;
You can try below code
private static String[] solution(String[] inputArray) {
int longestStrSize = 0;
List<String> longestStringList = new ArrayList<>();
for (int i = 0; i < inputArray.length; i++) {
if (inputArray[i] != null) {
if (longestStrSize <= inputArray[i].length()) {
longestStrSize = inputArray[i].length();
longestStringList.add(inputArray[i]);
}
}
}
final int i = longestStrSize;
return longestStringList.stream().filter(x -> x.length() >= i).collect(Collectors.toList()).stream()
.toArray(String[]::new);
}

Java How to return unique index of char in string?

I need to return unique value of character in string.
I'm using String.indexOf(), but it doesn't work correct for me.
For example: camera
indexes: c-0, a-1, m-2, e-3, r-4, a-5
String.indexOf() always returns index 1 for letter "a", not 1 and 5 as I need.
Any ideas how to solve it?
public static void main(String[] args) {
List<Integer> indexes = getIndexInString("camera", "a");
System.out.println(indexes);
}
public static List<Integer> getIndexInString(String str, String charToFind) {
List<Integer> indexes = new ArrayList<Integer>();
int i = -1;
while (true) {
i = str.indexOf(charToFind, i + 1);
if (i == -1)
break;
else
indexes.add(i);
}
return indexes;
}
Here you go.
public static void main(String[] args) {
String text = "camera";
String[] indexes = getIndexesOf(text);
for(String i : indexes) {
System.out.println(i);
}
System.out.println();
int[] result = indexOfChar(text, "a");
for(int i : result) {
System.out.print(i +",");
}
}
public static String[] getIndexesOf(String text) {
String[] indexes = new String[text.length()];
for(int i=0; i<text.length(); i++) {
indexes[i] = text.charAt(i) + "-" + i;
}
return indexes;
}
public static int[] indexOfChar(String text, String c) {
List<Integer> indexOfChars = new ArrayList<Integer>();
for(int i=0; i<text.length(); i++) {
if(String.valueOf(text.charAt(i)).equals(c)) {
indexOfChars.add(i);
}
}
int [] retIndexes = new int[indexOfChars.size()];
for(int i=0; i<retIndexes.length; i++) {
retIndexes[i] = indexOfChars.get(i).intValue();
}
return retIndexes;
}

Combining two ArrayLists [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 years ago.
Improve this question
I have to interleave two arraylists together into one arraylist, but I can't seem to work this out. The code should work when the size of list 1 is bigger, or when the size of list 2 is bigger.
I have yet to learn any methods or terminology that's not included in the code below.
Help would be much appreciated. Thank you! Here is what I have so far:
public static void interLeave(ArrayList<String> list1, ArrayList<String> list2)
{
ArrayList<String> tempList = new ArrayList<>();
int count = 1;
int length = list1.size() + list2.size();
String temp1 = "";
String temp2 = "";
boolean test = true;
if (list1.size() >= list2.size())
{
for (int j = 0; j<length; j++)
{
for (int i = 0; i < length ; i++)
{
tempList.add(2*i, list1.get(i));
tempList.add(2*i+1, list2.get(i));
count++;
if (count == list2.size())
{
break;
}
}
}
list1.clear();
list1.addAll(tempList);
}
else
{
for (int i = 0; i<length; i++)
{
list1.add(2*i+1, list2.get(i));
count++;
if (count == list1.size())
{
break;
}
}
}
public static void main(String[] args)
{
ArrayList list1 = new ArrayList();
list1.add(2);
list1.add(3);
list1.add(7);
ArrayList list2 = new ArrayList();
list2.add("Hi?");
list2.add("name");
list2.add("mallory");
list2.add("nice");
ArrayList list3 = new ArrayList();
list3.add("my");
list3.add("is");
list3.add("pizzazz");
interLeave(list2, list3);
System.out.print(list2);
How about (Just to simplify)
public static void interLeave(ArrayList<String> list1, ArrayList<String> list2)
{
ArrayList<String> tempList = new ArrayList<String>();
int length = Math.max(list1.size(), list2.size());
for (int j = 0; j<length; j++)
{
if (j < list1.size()) {
tempList.add(2*j, list1.get(j));
}
if (j < list2.size()) {
tempList.add(2*j+1, list2.get(j));
}
}
list1.clear();
list1.addAll(tempList);
}
Try this
import java.util.ArrayList;
public class Test{
public static void interLeave(ArrayList<String> list1,
ArrayList<String> list2) {
ArrayList<String> tempList = new ArrayList<>();
int count = 1;
int length = list1.size() + list2.size();
String temp1 = "";
String temp2 = "";
boolean test = true;
if (list1.size() >= list2.size()) {
for (int j = 0; j < list2.size(); j++) {
for (int i = 0; i < list2.size(); i++) {
tempList.add(2 * i, list1.get(i));
tempList.add(2 * i + 1, list2.get(i));
count++;
if (count == list2.size()) {
break;
}
}
}
list1.clear();
list1.addAll(tempList);
} else {
for (int i = 0; i < length; i++) {
list1.add(2 * i + 1, list2.get(i));
count++;
if (count == list1.size()) {
break;
}
}
}
}
public static void main(String[] args) {
ArrayList list1 = new ArrayList();
list1.add(2);
list1.add(3);
list1.add(7);
ArrayList list2 = new ArrayList();
list2.add("Hi?");
list2.add("name");
list2.add("irene");
list2.add("nice");
ArrayList list3 = new ArrayList();
list3.add("my");
list3.add("is");
list3.add("Lee");
interLeave(list2, list3);
System.out.print(list2);
}
}
Output is
[Hi?, my, name, is, irene, Lee, Hi?, my, name, is, irene, Lee, Hi?, my, name, is]
One more solution.
public static void interLeave(ArrayList<String> list1, ArrayList<String> list2)
{
ArrayList<String> tempList = new ArrayList<>();
Iterator<String> Iterator1 = list1.iterator();
Iterator<String> Iterator2 = list2.iterator();
while (Iterator1.hasNext() && Iterator2.hasNext())
{
tempList.add(Iterator1.next());
tempList.add(Iterator2.next());
}
while (Iterator1.hasNext()){
tempList.add(Iterator1.next());
}
while (Iterator2.hasNext()){
tempList.add(Iterator2.next());
}
list1.clear();
list1.addAll(tempList);
}

Convert List<List<String>> to String[]

I have a requirement wherein i should write a method of type String[] and return the same.But the implementation that i have, uses and returns List<List<String>>.Also the List<List<String>> gets and returns values from the database and the values are not known prior to add them to the String[] directly.The list can also be of a huge size to accomodate it in an String[] . How to get this conversion done.
This should work just fine. Though if you can return your embedded list structure that would be even better:
final List<String> resultList = new ArrayList<String>(64000);
final List<List<String>> mainList = yourFuncWhichReturnsEmbeddedLists();
for(final List<String> subList: mainList) {
resultList.addAll(subList);
}
final String[] resultArr = subList.toArray(new String[0]);
Take a look at this code:
public static String[] myMethod(ArrayList<ArrayList<String>> list)
{
int dim1 = list.size();
int total=0;
for(int i=0; i< dim1; i++)
total += list.get(i).size();
String[] result = new String[total];
int index = 0;
for(int i=0; i<dim1; i++)
{
int dim2 = list.get(i).size();
for(int j=0; j<dim2; j++)
{
result[index] = list.get(i).get(j);
index++;
}
}
return result;
}
Run this test code:
public static void main(String[] args)
{
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
ArrayList<String> list3 = new ArrayList<String>();
list1.add("first of first");
list1.add("second of first");
list1.add("third of first");
list2.add("first of second");
list3.add("first of third");
list3.add("second of third");
ArrayList<ArrayList<String>> superList = new ArrayList<ArrayList<String>>();
superList.add(list1);
superList.add(list2);
superList.add(list3);
String[] output = myMethod(superList);
for(int i=0; i<output.length; i++)
System.out.println(output[i]);
}

Categories