Clear a list after adding its elements to another list - java

I'm doing a program that gets as parameter more sentences. And I made 2 lists, one called "propozitie", which contains each sentence, and one called "propozitii", which contains all sentences.
The problem is: When I clear the "propozitie" list after it meets a ".", it also clears the "propozitii" list and it's ending up by printing 3 empty lists.
Input data:
Andrei goes there. Andrei plays football. I enjoy staying home.
Can anybody help me ?
Thanks in advance.
import java.util.ArrayList;
import java.util.List;
public class Propozitie {
private static List<List<String>> propozitii;
private static List<String> propozitie;
public static void main(String args[]) {
propozitii = new ArrayList<List<String>>();
propozitie = new ArrayList<String>();
for (String arg : args) {
propozitie.add(arg);
if (arg.contains(".")) {
propozitii.add(propozitie);
propozitie.clear();
}
}
System.out.println(propozitii);
}
}

This is because, the statement
propozitii.add(propozitie);
adds the reference to propozitie to propozitii list. A copy of the object is not added. To do a copy, use this.
propozitii.add(new ArrayList<String>(propozitie));
now you can clear propozitie

Related

How to add another linked list objects

I a homework where I need to merge two linkedlist, "songs" list and "artists" linked list, it works on the first list the "song" list, but whenever i add another list it just wont work
as you can see here the public static void now has errors
package mergedlinkedlist;
import java.util.LinkedList;
public class Mergedlinkedlist {
public static void main(String[] args) {
LinkedList<String>song = new LinkedList<>();
song.add("imagine");
song.add("bohemian rhapsody");
song.add("highway to hell");
System.out.println(song);
}
public static void main(String[] args) {
LinkedList<String>artists = new LinkedList<>();
artists.add("john lennon");
artists.add("queen");
artists.add("ACDC");
System.out.println(artists);
}
Okey, as one comment has pointed out, no program can have 2 main methods. You cannot create another one.
I suggest to integrate the code from your second into your first. You can safely just add the contents together.
The task is to merge the lists. Do you need any more help with that?
Assuming the task says you know the size of the list you can just create a new list and use the same method you used above to add them to the list. If you do not, you will need to use a loop. It would look like this:
LinkedList<String>artistsAndSongs = new LinkedList<>();
if (artists.size() == songs.size()) {
for (int i = 0; i < artists.size(); i++) {
artistsAndSongs.add(artists.get(i));
artistsAndSongs.add(songs.get(i));
}
} else {
System.out.println("Not every artist is matched with a song!");
}
But you should read up on how linked lists work exactly in the documentation. This will not only improve your understanding of the class but also teach you how to quickly find solutions to your problem.
https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html

Code not working as expected. Using ArrayList and for loop in my Selenium Webdriver Java code

So I have recently started my work on Selenium Webdriver using Java. Below is the code:
List<LineDashboardDetails> LineDashboardDetailList = new ArrayList<LineDashboardDetails>();
LineDashboardDetails objLineDetail = new LineDashboardDetails();
for (int i = 0; i < lineUITextList.size(); i++) {
objLineDetail.lineName=lineUITextList.get(i);
objLineDetail.machineNames = machineUITextList.get(i).toString();
LineDashboardDetailList.add(objLineDetail);
}
System.out.println(LineDashboardDetailList);
I have created a lineUITextList ArrayList of String. This array will always have only 1 value in it. So the issue is above for loop does not work. The loop only executes once and comes out to print the LineDashboardDetailList. The machineUITextList array has around 5-6 values in it. My expectation is to have LineDashboardDetailList such that the lineUITextList common value is paired with each new value of machineUITextList.
For example if lineUITextList= {"Noun"}
machineUITextList= {"Pen","Box","Note","Scale"}
So my list i.e LineDashboardDetailList should give me output as:
Noun,Pen
Noun,Box
Noun,Note
Noun,Scale
I am using LineDashboardDetailList List further in my code.
Thanks in Advance.
I am not totally sure what you are trying to achive. Personally, I would use a java.util.Map to associate each value of the lineUITextList with the list of values of machineUITextList.
However, to help you in achiving your goal, first of all, I would design the LineDashboardDetails class in order to maintain the single value of the lineUITextList, along with the list of machineUITextList, so as you can combine them by using a specific method. That has lots of advantages in terms of encapsulation, distribution of responsabilities, etc.., plus you can always reuse for other purposes.
The function to combine the values can be easily implemented by taking advantages of Java stream and built-in functional interfaces.
Here is the code of the LineDashboardDetails class:
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;
public class LineDashboardDetails {
private String lineName;
private List<String> machineNames;
public String getLineName() {
return lineName;
}
public void setLineName(String lineName) {
this.lineName = lineName;
}
public List<String> getMachineNames() {
return new ArrayList<>(machineNames);
}
public void setMachineNames(List<String> machineNames) {
this.machineNames = new ArrayList<>(machineNames);
}
public List<String> getCombinedList() {
return machineNames.stream()
.map(s -> lineName + "," + s)
.collect(Collectors.toList());
}
}
Here is instead the code that you tried to implement, which uses the class above and combine the two list, and finally prints out the list of values as you expect. You can see, I prepared two simple list in the main method, but you can actually generalize it in relation to your own needs:
import java.util.List;
import java.util.ArrayList;
public class SeleniumWebdriverListTest {
public List<LineDashboardDetails> combineUITextListWithMachineUIText(List<String> lineUITextList,
List<String> machineUITextList) {
List<LineDashboardDetails> lineDashboardDetailList = new ArrayList<>();
for (String lineUIText : lineUITextList) {
LineDashboardDetails objLineDetail = new LineDashboardDetails();
objLineDetail.setLineName(lineUIText);
objLineDetail.setMachineNames(machineUITextList);
lineDashboardDetailList.add(objLineDetail);
}
return lineDashboardDetailList;
}
public static void main(String[] args) {
List<String> lineUITextList = new ArrayList<>();
lineUITextList.add("Noun");
List<String> machineUITextList = new ArrayList<>();
machineUITextList.add("Pen");
machineUITextList.add("Box");
machineUITextList.add("Note");
machineUITextList.add("Scale");
List<LineDashboardDetails> lineDashboardDetailList =
new SeleniumWebdriverListTest().combineUITextListWithMachineUIText(
lineUITextList, machineUITextList);
lineDashboardDetailList.stream()
.map(s -> s.getCombinedList())
.forEach(System.out::println);
}
}
I could have used Java streams and a specific lamba expression to implement the combineUITextListWithMachineUIText method as well, but I kept somehow your original version to let you understand my idea of implementation around your code.
Here is the output I get:
[Noun,Pen, Noun,Box, Noun,Note, Noun,Scale]
Feel free to ask for any clarification.

Adding to an array list within a class in Java? Beginner

Still learning the basics however I've been stuck on this. I'm trying to add members to a club from the methods I've wrote, but I'm getting an error on the join method..
import java.util.ArrayList;
public class Club
{
private ArrayList<String> memberList;
public Club()
{
memberList = new ArrayList<String>();
}
public void join(String newMember)
{
memberList.add(newMember);
}
}
public class TestClub
{
public TestClub()
{
}
public static void main(String args[]){
Club myClub = new Club();
System.out.println("The club has " +
myClub.numberOfMembers() +
" members.");
myClub.join("Gary", 5, 2019));
}
}
I know I'm missing something, just can't figure out what. I create the club object but it won't let me add to the list. How do I get the object to accept 3 values/arguments?
This line:
myClub.join("Gary", 5, 2019));
You have two closing parentheses. You might want to delete the last one. Also, you have 3 variables, the string, the integer and another integer.
Maybe you want to say:
myClub.join("Gary");
or
myClub.join("Gary, 5, 2019");
Also, you haven't yet implemented the method, numberOfMembers in the Club class, so that will always fail.
myClub.numberOfMembers()
You would need to have a method in Club class that returns the number of members, meaning the memberList size.
1)You have added extra closing ')' also method 'join(String newMember)' has one parameter, the String. You are trying to pass it 3 value :- join("Gary", 5, 2019);
2) Also your arrayList is of type String you can only add String values eg : "Gary" and not Integer eg : 5 or 2019.
You can also redefine method 'joins' like below for adding multiple value in single method call
public void join(String ...newMember)
{
Collections.addAll(memberList, newMember);
}
and change method call as below
myClub.join("Gary", "5", "2019");

How do I use on method to get the sizes of two different-sized arrayslists?

I'm new to Java and I want to use one method, getCount, to get the sizes of two arraylists that are not the same size. What code could I use so that I can call, for example, examp1.getCount and examp2.getCount, and get the two different sizes?
This is frankly as basic a question as it gets, and google could have helped you with this easily. Here's your entire method if you want to design your own.
public static int getCount(ArrayList A){
return A.size();
}
In your method, use .size() to get the sizes of the two ArrayLists
and store the sizes in the array and then return that array.
Suppose you create 2 lists li1 and li2 then :
package output;
import java.util.ArrayList;
import java.util.List;
public class CalculateLength {
public static void main(String[] args) {
List<Integer> li1= new ArrayList<>();
li1.add(1);
li1.add(2);
getCount(li1);
List<Integer> li2= new ArrayList<>();
li2.add(1);
li2.add(2);
li2.add(3);
li2.add(4);
getCount(li2);
}
public static int getCount(List list) {
System.out.println("LEngth = " + list.size());
return list.size();
}
}

Converting Vector to HashSet in Java

Could someone explain, how the following code printed the Vector elements (in which order they were printed)?
Code:
import java.util.HashSet;
import java.util.Vector;
class Test
{
public static void main(String [] args) throws Exception
{
Vector data = new Vector();
data.add("apple");
data.add("mango");
data.add("papaya");
data.add("cherry");
data.add("banana");
data.add("apple");
System.out.println(getData(data));
}
public static Vector getData(Vector v)
{
return new Vector(new HashSet(v));
}
}
Output:
[banana, cherry, papaya, apple, mango]
Hashset doesn't store elements in a user specified Order. As soon as you created the Hashset using Vector, Elements lost the specified order.
Moreover they don't allow duplicates, so the second apple got lost.

Categories