This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 7 years ago.
I would like to write a linked list like this:
"a" -> "b" -> "c" -> "d"
This is what I've tried so far but it's obviously wrong. I was wondering how to express this correctly in java?
LinkedList<String> s = new LinkedList<>();
s = {"a"->"b"->"c"->"d"};
Thanks!
That's how the pointers in the list look internally, to actually add it to the list you need to do this:
List<String> s = new LinkedList<>();
s.add("a");
s.add("b");
s.add("c");
s.add("d");
Take a look at this answer.
LinkedList<String> list = new LinkedList<>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
If you really want it on one line:
LinkedList<String> list = new LinkedList<>(Arrays.asList("a","b","c","d"));
Though that does have a performance overhead.
You could do this:
LinkedList<String> linkedList = new LinkedList<String>();
linkedList.add("a");
linkedList.add("b");
linkedList.add("c");
linkedList.add("d");
Related
This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 3 years ago.
I found a problem when I am using java generics.
List list = new LinkedList<String>();
list.add(MyObject); //No
prompts for any compilation errors.This is not I want
If I use :
List<String> list = new LinkedList<>();
list.add(MyObject); //Prompt for compilation errors.This is what I want
I wanna know what is the difference between
List<String> list = new LinkedList<>() and
List list = new LinkedList<String>() and
List<String> list = new LinkedList<String>() and
List<String> list = new LinkedList()?
List<String> list = new LinkedList<String>(); does the following:
Declare a variable called list with type List<String>
Call the constructor of LinkedList with the type parameter String
Sets the value of list to the result of step 2.
Since a List<String> is obviously not going to be given a new LinkedList<Elephant>();, it is OK to remove the type parameter from the second part, giving: List<String> list = new LinkedList<>();. This is called "Type Inference". Java can only do this when it can calculate at compile-time what the omitted type would be.
If you use List list = new LinkedList<String>();, you do exactly the same thing as above, except your new variable list does not contain type information. This can be dangerous, as it prevents the compiler from warning/stopping you when you do something that would cause a type error.
For example:
List<String> list = new LinkedList<>();
list.add("hello"); // works fine
list.add(123); // compile-time error
I have been saved from putting an int into a list of Strings. However, if using a regular List:
List list = new LinkedList<String>();
list.add("hello"); // stil works
list.add(123); // also works
The issue with this comes from when you then retrieve items from the list:
List list = new LinkedList<String>();
list.add(123); // allowed
Object obj = list.get(0); // is obj a String? is it an int?
This breaks type safety, which is arguably a strong reason for using Java in the first place.
This question already has answers here:
Java ArrayList copy
(10 answers)
Closed 5 years ago.
Here is what I have to do:
Instantiates a new list array and copies the elements of the existing list array to the new list array. The list array instance variable becomes a pointer to the new list array.
I am working with array lists and methods. I have an array list with some numbers. I took numbers from that list but I don't know how to put the numbers that I took into a new array list.
HI Please check the below code.
ArrayList<Integer> oldlist = new ArrayList<>();
oldlist.add(0);
oldlist.add(1);
oldlist.add(2);
oldlist.add(3);
ArrayList<Integer> newList = new ArrayList<>();
newList.addAll(oldlist);
This is a basic problem in programming. It is usually called "iterating" or more specifically here: "iterating through / over a collection".
A classic way in Java would be:
ArrayList<Stuff> list2 = new ArrayList<>();
for (Stuff thing : list1) list2.add(thing);
You can read the second line as: "For each thing, of class Stuff, in list1, do..." The thing you want to do, is calling addon the second list.
As of Java7 you could do:
list1.forEach((thing) -> list2.add(thing));
Or even simpler:
ArrayList<Integer> newList = new ArrayList<>(oldList);
This question already has answers here:
Java Compare Two Lists
(11 answers)
Closed 7 years ago.
I need to check whether elements of one list or array exists in other list.
The size of lists are same.
Apart from sorting is any simple solution possible?
You are right in saying that if they are arrays you can't use Arrays.equals(), if they are List<>s then you cannot just use equals. In both cases they also check order.
So yes your main option without writing your own comparison algorithm is to use Collections.sort() on both lists.
If you don't need to check for duplicates you could drop both lists into a HashSet using addAll and then compare the two HashSet or just use List.containsAll. The bad news though is that these would both have the same limitation, if you need to compare lists that might have repeated elements then it may give incorrect results. i.e. "bob", "bob", "fred" would compare as equal to "bob", "fred", "fred".
Please refer to the example below:
public class Example{
public static void main(String[] args){
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<String>();
list1.add("A");
list1.add("B");
list1.add("C");
list2.add("C");
list2.add("X");
list2.add("Y");
for(String s : list1){
if(list2.contains(s)){
System.out.println("List 2 contains: " + s);
}
}
}
}
The code above is by all means not the cleanest or the most compact way to achieve what you are asking. But given the information presented, this should suffice.
To check if your an item of list1 is in list2 you could use this code:
ArrayList<Integer> list1 = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
//Adding some object to the list here
int yourIndex = 1;
if(list2.contains(list1.get(yourIndex)))
{
//do what you want
}
This question already has answers here:
Initialization of an ArrayList in one line
(34 answers)
Closed 8 years ago.
Sorry i really dont know how to do this, last resort but to ask.
I wanted to add values to the string list. But my code is not working
private List<String> sample = new ArrayList<String>(){"item1","item2","item3"};
Here it is:
List<String> sample = new ArrayList<String>(Arrays.asList("item1", "item2", "item3"));
Try,
ArrayList<String> list = new ArrayList<String>() {{
add("item1");
add("item2");
add("item3");
}}
OR
ArrayList<String> list = new ArrayList<String>();
list.add("item1");
list.add("item2");
list.add("item3");
This question already has answers here:
How do I remove objects from an array in Java?
(20 answers)
Closed 9 years ago.
I currently have an Arraylist as shown below.
T[] v = { v1,v2, v3, v4 };
I also have another array list:
removeT[] x = {v2, v4}
From the second, I would like these two values to be removed from the initial Array list. What are the required steps?
The best way to do it is something like this:
for (String each : removeT) {
if(v.equals(each)){
v.remove(each)
}
}
You can find more information on ArrayLists here.
I don't know of any operation that will do this directly on an array, so the solution I have to convert the arrays to a list:
String[] v = new String[]{ "v1", "v2", "v3", "v4" };
String[] x = new String[]{ "v1", "v4" };
List<String> list1 = new ArrayList<String>();
list1.addAll(Arrays.asList(v));
List<String> list2 = Arrays.asList(x);
list1.removeAll(list2);
and then when you are finished convert the list back to an array.
The problem of doing this directly on an array is that you would end up with null entries, which may create other issues, depending on your usage.