Having trouble understanding how to create an array of linkedlists - java

Ok, so I have been reading every google result about creating an array of linkedlist, and most of the stack overflow threads, but I don't really understand what they are doing. Do I need to create a seperate class that extends linkedlist and then create an array of that class?
I have tried a million different ways of arranging this code, but this is what I have at the moment.
public static int[][] genPerms(int numElements, int totPerms) {
int permArray[][] = new int[totPerms][numElements];
LinkedList<Integer>[] elementsLeftList = new LinkedList<Integer>[numElements];
The error is generic array creation. Can someone explain to me what is actually going on here.
In addition to the solutions below I was told you can create an array of head pointers.
Thanks in advance.

It's not allowed to create generic arrays, do the following
#SuppressWarnings("unchecked")
LinkedList<Integer>[] elementsLeftList = new LinkedList[numElements];
it works OK

You can't currently create an array of generics in Java without going through a complicated process. Can you do the following instead?
List<LinkedList<Integer>> elementsLeftList = new ArrayList<LinkedList<Integer>>();
If you really need it as an array you can then get it from elementsLeftList.toArray() and cast the result.
You can read the following link for the explanation: http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#FAQ104

Related

creating lists in java

So I am pretty new to java, and am trying to create a list in java with this:
private creatureKind[] field = new creatureKind[7];
creatureKind being another class I created within the same package. Is this the right syntax? I am trying to call functions such as set(), which
I found on this link: https://docs.oracle.com/javase/8/docs/api/java/util/List.html#set-int-E-, but I am getting an error message that abridged is saying that field is an array type. Not a frequent poster of this site so sorry if I messed stuff up in advance.
What you defined is a static array with a 7 element.
If you want to define a list, or better, an ArrayList you should it as follows:
List<creatureKind> list = new ArrayList<>();
Note that this is an unbound list, you should add values, before setting values. In general, I would suggest reading the documentations: https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html
If I understood your question correctly, you might need to do the following:
List<creatureKind> myList = new ArrayList<>();

How can I add an Array into a Queue?

Is it possible to add an array into specifically, a ConcurrentLinkedQueue? If so, how exactly would the .add statement look like?
THIS IS HOMEWORK RELATED, however, my entire program is meant to perform calculations on MyObjects (the default constructor of MyObjects generates random values to perform a ton of calculations on).
For example:
//Note: I couldn't use the Diamond Braces for the Queue-type when posing this question
ConcurrentLinkedQueue theLinkedQueue {MyObjects[]} =
new ConcurrentLinkedQueue{MyObjects[]}();
MyObjects[] theArray = null;
for(int i = 0; i < 100; i++){
theArray = new MyObjects[CONSTANT_SIZE];
theLinkedQueue.add(theArray(objparameter1, objparameter2));
}
The program implements multi-threading and in my thread class I've passed the Queue into the constructor, and am attempting to take off a MyObject array which a MyObject temp will point to, but so far I'm only capable of adding a single MyObject to my Queue at a time and pulling it. I want to be able to add the same amount of MyObjects as individual components rather than individually. I've attempted various lines of code only for NetBeans IDE to recommend a method to throw an UnsupportedOperation Exception. How could I add arrays into my ConcurrentLinkedQueue?
(Also apologies if my question is dense or confusing, first time posting here).
The correct syntax for the declaration of your queue is:
ConcurrentLinkedQueue<MyObjects> theLinkedQueue = new ConcurrentLinkedQueue<>();
Start with that and see how things go from there.
I figured out the solution, which was simply to add the array without 'objparameters' included.
theLinkedQueue.add(a); //where a was a 'MyObject' array.
I presumed you had to load the parameter to be passed for each array index, which seems pretty silly.

How to combine array with arraylist in Java?

I need a 5-dimensional data structure in Java with "double" as type for all the cells. For 3 of dimensions I know the size, so it fits to array category. But for 2 of dimensions I don't know the size beforehand; looks like ArrayList.
I have been able to manage the combination for 3 dimensions in the past:
ArrayList(Double)[][] prXifY = (ArrayList(Double)[][]) new ArrayList[m][n];
But despite long hours working on it (and through search in the net), I wasn't able to scale it. I need something like this:
ArrayList(ArrayList(Double))[][][] prXiXjY = (ArrayList(ArrayList(Double))[][][]) new ArrayList(ArrayList<Double))[m][m][n];
When I tried the above code, it says:
"Cannot create a generic array of ArrayList(ArrayList(Double))"
I will appreciate quick and complete answers.
By the way, this is my very first post ever. I tried my best to do a good job on searching beforehand and explaining the problem clearly. Comments on these matters are appreciated as well. :)
An ArrayList is an object, and instantiated differently than an array is. In general, to say that you want an ArrayList that holds doubles, you might use something like:
ArrayList<Double> list = new ArrayList<Double>();
to specify that you have an ArrayList which holsts ArrayLists which hold doubles...
ArrayList<ArrayList<Double>> list = new ArrayList<ArrayList<Double>>();
You see where this is going, I hope.
This just creates the top-level list - it doesn't create any of the cells themselves. For that, you'll need additional new statements. This is going to get messy fast, and you may want to stop and consider if there is a better way to store the data than in a 5-dimension array.
I think what you want would look something like this
List<List<List<List<List<Double>>>>> myList= new ArrayList<List<List<List<List<Double>>>>> ();
as you can tell this looks insane and will be very hard to maintain. You should probably look at alternative methods of doing this.
Multi dimensional arrays can be created like so:
ArrayList<Double> oneDimensionalArrayList = new ArrayList<>();
ArrayList<ArrayList<Double>> twoDimensionalArrayList = new ArrayList<>();
ArrayList<ArrayList<ArrayList<Double>>> threeDimensionalArrayList = new ArrayList<>();
ArrayList<ArrayList<ArrayList<ArrayList<Double>>>> fourDimensionalArrayList = new ArrayList<>();
ArrayList<ArrayList<ArrayList<ArrayList<ArrayList<Double>>>>> fiveDimensionalArrayList = new ArrayList<>();
But I would definitely recommend considering whether a 5 dimensional array is what you require for the problem at hand; it smells like something is wrong

How to create ArrayList in each index of an array in Java?

I am trying to implement bucket sort and I want to create an arraylist for each index of an array. I want to do something like this:
int bucket[]=new int[max+1];
for(i=0;i<=max/5;i++)
{
bucket[i]=new ArrayList<Integer>();
}
But the above code doesn't work and throws an error. It states "generic array creation".I know that generic array types are not allowed in java but I can't understand where generic types comes here I mean something like or .I also want to know is there a way to cast Integer to int while creating the ArrayList I hope doing so will fix this.
Your data types do not match...
You are trying to put an ArrayList<Integer> into a int[]. It cannot hold this type of data!
please reconsider what you want to have:
int[][]
ArrayList<int[]>
ArrayList<ArrayList<Integer>>
ArrayList<Integer>[] <--- thi is not well-supported due to the way generics work
If you want higher performance, you should have a look at GNU Trove, and maybe use
ArrayList<TIntList>
which should use much less memory than ArrayList<ArrayList<Integer>>.
If your data size is fixed, you probably are looking for
int[][] bucket = new int[max+1][max/5+1];

Java Returning a New Object *Complete Beginner

I am going to ask hopefully an easy question and I apologize in advance for any wording issues. I have a custom products class which works without an issue, within another class I create an array of products. see below:
products = new product[10];
product_count = 0;
I now wish to write a method to return not only the array of products but also the product_count, is it possible to return both as a new object? - if not could someone even show me how to return an object. I apologise if this seems a stupid question I am merely trying to learn, and thanks for any advice in advance
**thanks for all the responses - now I will throw a spanner in the works - I am trying to implement this method using java RMI - does anyone have an idea of how this would function?
You can use this to return your array:
public product[] returnArray(){
return products;
}
Keep in mind that you can just update your product_count instead of returning it.
Arrays know their own length. There's no need to return a length and the array separately. Just read the length field:
Product[] products = new Product[10];
System.out.println("The size of products is " + products.length);
Note that I capitalized the type name Product. Common convention in Java is for all class names to start with an uppercase letter.
Returning a single object can be done using return: return referenceName;
Returning multiple objects is easiest if you create your own class for it, or check for already existing implementations that might fit your use case. For example see: http://docs.oracle.com/javase/7/docs/api/java/util/AbstractMap.SimpleImmutableEntry.html

Categories