LinkedList<class> merge with more than 2 - java

I doing small game with pairs of images than need to be paired and I got in to trouble with merging all LinkedList that they object is classes that posses constructor. They store x,y and cropped img. What I can do to merge this lists to one, when node doesn't work. My goal is to have one LinkedList that will shuffle it over the frame.
What I got
and what i try to achive
Can't do this with addAll ,as i have few LinkedList that refer to different classes. Every class have different img and animation to it.
private LinkedList<Odkryte00> os = new LinkedList<Odkryte00>();
private LinkedList<Odkryte01> os2 = new LinkedList<Odkryte01>();
private LinkedList<Odkryte02> os3 = new LinkedList<Odkryte02>();
private LinkedList<Odkryte03> os4 = new LinkedList<Odkryte03>();
private LinkedList<Odkryte04> os5 = new LinkedList<Odkryte04>();
private LinkedList<Odkryte05> os6 = new LinkedList<Odkryte05>();

It sounds like you want to merge multiple LinkedLists together and randomize the items in the combined list so you can shuffle the pieces of the board in the image you show. This is very easy to accomlish in Java and there are already built-in methods to do so using Collections.
Noting from your update
You want to have a LinkedList of various types of objects as your final list, this is still very possible to accomplish using addAll() there just need to be a few edits from what I was showing earlier.
To do this we return:
LinkedList<Object>
From the combineAndShuffle() method I put together earlier, like so:
import java.util.Collections;
import java.util.LinkedList;
public class Test{
#SafeVarargs
public static LinkedList<Object> combineAndShuffle(LinkedList<?>... l){
//must be <Object> because it needs to hold multiple types of objects
LinkedList<Object> finalList = new LinkedList<>();
//use <?> wildcard because we are unsure what value we will hit
for(LinkedList<?> list: l) {
finalList.addAll(list);
}
//shuffle the list
Collections.shuffle(finalList);
return finalList;
}
public static void main(String [] args){
//list of strings
LinkedList<String> listA = new LinkedList<>();
listA.add("One");
listA.add("Two");
//list of integers, so a different class type
LinkedList<Integer> listB = new LinkedList<>();
listB.add(3);
listB.add(4);
LinkedList<Object> combinedList = combineAndShuffle(listA, listB);
for(Object item: combinedList)
System.out.println(item);
}
}

Related

Android extract array of properties of array of objects

I was wondering if there's an effective way to extract an array of properties from an array of custom class objects. For example if I have something like this:
public class MyClass {
private Double p1;
private String p2;
private MyProperty p3;
public MyClass() {}
}
and in somewhere I have an ArrayList filled with objects of this class:
ArrayList<MyClass> listOfObjects = new ArrayList<>();
and I'd like to get a list of one of the properties:
ArrayList<MyProperty> listOfP3 = new ArrayList<>();
ArrayList<Double> listOfP1 = new ArrayList<>();
All I can think of is iterating through listOfObjects and copying the desired properties one by one to a new array... Is there a better way to do this?
EDIT:
If possible one that works with Java 7 also
Probably the cleanest way to do it is to use Streams. Something like this:
List<String> listOfP2= listOfObjects.stream().map(x->x.getP2()).collect(Collectors.toList());
Of course in MyClass you need to add a getter for those fields.
In Java 8 and higher, you can utilize the stream API (as #Amongalen already answered). There is a different possibility of accessing methods: Instead of x -> x.getP1() you can just write MyClass::getP1:
List<Double> p1List = myObjects.stream().map(MyClass::getP1).collect(Collectors.toList());
List<String> p2List = myObjects.stream().map(MyClass::getP2).collect(Collectors.toList());
List<MyProperty> p3List = myObjects.stream().map(MyClass::getP3).collect(Collectors.toList());

How to pass a linked list in Java?

I am having trouble understanding how to pass a linkdlist into a method that display that list as a stack. I am aware i havent specifed the type of list but my instructor said that it would not matter for this purpose. but I am still learning so I'm not to sure if I am passing the linkedlist correctly into the method.
import java.util.LinkedList ;
import java.util.ListIterator;
public class UseStacksAndQueues{
public static void main(String[] args) {
StacksAndQueues sQ = new StacksAndQueues();
String [] days = {"mon","tue","wed","thur","fri", "sat","sun"};
LinkedList aList = new LinkedList();
LinkedList newList = new LinkedList();
//load array of string objects into linked list
aList = sQ.methodOne(days);
//display linked list as a stack
sQ.methodTwo(aLits);
the method.
//display a linked list as a stack
public LinkedList methodTwo(aList){
for(int i = aList.size; i <= 0; i--)
{
System.out.println(aList.get(i));
}
}//end method two
Your call to the method is correct. The method itself is the problem. You need to specify the type of object being passed into your method.
public LinkedList<String> methodTwo(LinkedList<String> aList){
...
}
You also need to specify the type of your LinkedList in angled brackets, as shown above. That includes when you create your list before passing it around.
LinkedList<String> aList = new LinkedList<>();
The second pair of angled brackets can be empty, as shown above. This is a shortcut introduced in Java 7.
that would be correct. in java, objects are passed by reference (except for primitive types) so you will be able to perform all of the operations on the list you pass to methodTwo.
Please refer to this post for an explanation
Is Java "pass-by-reference" or "pass-by-value"?
Having said that, you do not have any types associated with your List objects, so you will need to specify that.
so something like
public void methodTwo(LinkedList<String> aList){
for(int i = aList.size; i <= 0; i--)
{
System.out.println(aList.get(i));
}
}//end
i have the return type as void, as that is all thats needed if you only need methodTwo for display purpose
and you will also need to declare as
LinkedList<String> aList = new ArrayList<String>();

java generics wildcard compiling error (generic of generic)

Hello I have a compiling problem with this peace of code. How can I perform a safe add to data variable?
import java.util.*;
public class Foo
{
private TreeSet<? extends Collection<String>> data;
public Foo()
{
data = new TreeSet<ArrayList<String>>();
data.add("Goofy"); //this action generates a compile error
}
}
You're trying to add a String to a TreeSet of ArrayLists of Strings. You would need to add an ArrayList. Probably
ArrayList<String> list = new ArrayList<>();
list.add("Goofy");
data.add(list);
That is, assuming you're not using an overcomplicated design, which you very much probably are.
data is a collection of ArrayList and you are trying to add a String
You need to add the String to an array list first
ArrayList<String> list = new ArrayList<String>();
list.add("Goofy");
data.add(list);
or change data to be a TreeSet of Strings
private TreeSet<String> data;
data = new TreeSet<String>();
data.add("Goofy");

Linked List of Linked Lists in Java

I would like to know how to create a linked list of linked lists. Also, It would be helpful if the predefined LinkedList (class from Java) and its methods are used for defining and for other add, get, listIterating operations.
You can put any object in a list, including another list.
LinkedList<LinkedList<YourClass>> list = new LinkedList<LinkedList<YourClass>>();
is a LinkedList of LinkedLists of YourClass objects. It can also be written in a simplified way since Java 7:
LinkedList<LinkedList<YourClass>> list = new LinkedList<>();
Very simple examples of manipulating such a list:
You then need to create each sublist, here adding a single sublist:
list.add(new LinkedList<YourClass>());
Then create the content objects:
list.get(sublistIndex).add(new YourClass());
You can then iterate over it like this (sublists' items are grouped by sublist):
for(LinkedList<YourClass> sublist : list) {
for(YourClass o : sublist) {
// your code here
}
}
If you want to add specific methods to this list of lists, you can create a subclass of LinkedList (or List, or any other List subclasses) or you can create a class with the list of lists as a field and add methods there to manipulate the list.
Well i've done this code and i've got it right
java.util.LinkedList mainlist = new java.util.LinkedList();
java.util.LinkedList sublist1 = new java.util.LinkedList();
sublist1.add(object1);
sublist1.add(object2);
sublist1.add(object3);
java.util.LinkedList sublist2=new java.util.LinkedList();
sublist2.add(1);
sublist2.add(2);
mainlist.add(sublist1);
mainlist.add(sublist2);
// To retrieve the sublist1 from mainlist...........
java.util.LinkedList temp = (java.util.LinkedList)mainlist.get(0);
Here variable mainlist is LinkedList of LinkedLists and variable temp contains the value the first list stored i.e sublist1..
You can even simplify access to the secondary lists, e.g. using
final List<List<String>> lists = new LinkedList<List<String>>() {
#Override
public List<String> get(final int index) {
while (index >= size()) {
add(new LinkedList<>());
}
return super.get(index);
}
};
This code automatically adds new LinkedLists to the outer list. With this code you can later easily add single values:
lists.get(2).add("Foo");
LinkedList<LinkedList<YourClass>> yourList = new LinkedList<LinkedList<YourClass>>();
As the declaration. To add another linked list (to the end by default) you would do
yourList.add(new LinkedList<YourClass>());
To add an element to lets say the second linked list in the series:
yourList.get(1).add(new YourClass());

array of pair, ArrayList in java

how can I make array of ArrayList or Pair Class which I made myself at the code below.
ex1)
import java.util.*;
class Pair{
static int first;
static int second;
}
public class Main{
public static void main(String[] args){
Vector<Pair>[] v = new Vector<Pair>[100](); //this gives me an error
}
}
1.why the code above gives me an error?
2.my goal is to make an array of vector so that each index of vector holds one or more Pair classes. How can I make it?
another example) : array of ArrayList
import java.util.*;
public class Main{
public static void main(String[] args){
ArrayList<Integer> arr = ArrayList<Integer>(); //I know this line doesn't give error
ArrayList<Integer>[] arr = ArrayList<integer>[500]; // this gives me an error
}
}
3.why does the code above give me an error?
4.my goal is to make an array of ArrayList so that each index of Array has ArrayList/Queue/Vector/Deque whatever. How can I make it?
How about a full generic solution:
ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
The syntax you have used is not what Java uses. If you want to have an array of ArrayLists then do:
ArrayList[] arr = new ArrayList[100];
for(int i=0; i<arr.length; i++)
{
arr[i] = new ArrayList<Pair>(); // add ArrayLists to array
}
Here the type argument <Pair> specifies that the ArrayList should contain items of type Pair. But you can specify any type you wish to use. The same goes for ArrayList, you could replace ArrayList with Vector in the example.
It would be best to use an ArrayList instead of an array in the example. Its much easier to maintain without worrying about the changing length and indexes.
Hope this helps.
public static void main(String[] args){
Vector[] v = new Vector[5];
for(int i=0;i<5;++i){
v[i]= new Vector<Pair>();
}
}
I don't know java that well, but don't you want to do:
ArrayList<ArrayList<Pair>> v = new ArrayList<ArrayList<Pair>>();
Try to break down what containers you need in your question. Your goal is to make a ArrayList (ok, the outer ArrayList satisfies that purpose) that has one or more pair classes in that. "That has" means that "each item in the ArrayList is this type". What would we use to store one or more Pair classes? Another Vector/List of tyoe Pair. So each item in the outer ArrayList is another ArrayList of Pairs.
Note: I moved everything to ArrayList because I read that Vector is somewhat deprecated and they serve similar functions. You may want to check on this.
This example should help with with the next part of your question, but let me know if it doesn't,

Categories