Shuffle only one variable of objects in arraylist - java

I have an arraylist filled with objects of a class. These objects have variables like x,y and a imagePath. I only want to shuffle the imagepath variables in this arraylist between the objects. So x- and y variables of the objects should stay the same. How can I do this?
I know I can use collections.shuffle() to shuffle the entire arraylist. But this doesn't solve my problem.

Use one ArrayList for the objects and another for the images, shuffle the one with the images, and then make each object refer to the image on its same relative position.

Related

Java, Passing objects from one arraylist to another

I have a class with two ArrayLists that store two different types of objects
I have another class - SortedArrayList - which is responsible for sorting the above arraylists alphabetically and printing them to the console or to the file
I was wondering if there is any way for the SortedArrayList to acquire the added objects from the previous ArrayLists without adding them again to the SortedArrayList?
Please point me in the right direction
Thank you
If the objects are similar, you can use an interface so that the SortedArrayList can work with that and then will be able to work with both Objects.

When to use ArrayList and when to use simple integer array

Can anyone please explain in which situation are we supposed to use ArrayList instead of a simple array and what is the difference between these two and how to initialize the ArrayList.
I am new to java so use example if possible.
Whenever you don't know the size to be created, you can use ArrayList.
If you just want speed access, you can use array (indexing in array is faster than ArrayList).
And also, ArrayList reduces the complexity of coding.
ArrayList can be initialized like this
ArrayList al = new ArrayList();
Add values to the ArrayList
al.add("String");
al.add(number);

How to create an array that extends itself dynamically?

How to create an array that extends itself. I don't want to use the classes like ArrayList and Vector etc to do this. Instead i need to generate an array that extends it's size upon adding elements to it. This is question by my teacher.
Say for example, i want an int[] which extends it's size.
For instance, the user want to enter the student IDs into an array. The array has no fixed size since there are no fixed no. of students in this case. When the user says he wants one more, the array's size should be incremented by one.
Any answer is appreciated.
Arrays are fixed in length, you can not increase or decrease the size of array.
What you can do create new array with larger size and copy the values using Arrays#copyOf source array to new destination array.
Note: Arrays#copyOf internally call System.copy which does shallow copy.
Here is a useful link for your teacher, from the docs:
An array is a container object that holds a fixed number of values
of a single type. The length of an array is established when the array
is created. After creation, its length is fixed.
The only option to do that without ArrayList/Vector.. is creating a new array and copying the values to it.
Your description, 'When the user says he wants one more, the array's size should be incremented by one.' is just a pointer array, which is LinkedList in java.
Whatever your teacher says there is no way to resize a array dynamically without creating a new array with edited size. I don't think any language supports this requirement. Just create a new array and copy the existing one.
I think i'll have to re-initialize the array with an incremented size but before that, i think i'll have to copy all those elements into a temporary array and then again copy them into the original array whose size is changed.
If this is correct, my teacher might be looking for this. But that degrades the performance though.

Making a replica 2D array before I change it

I've made a 2D array which is a map for a game. As the player moves around the map the Array changes slightly, but I'd like to be able to refer back to the original, unchanged Array. How can I do this?
Thanks a lot.
If you don't need a copy of each element, just the array, use
array.clone()
If you do need to copy each element, you can see this answer.
If arr is a 2d String array:
String[][] copy = arr.clone();
Then, just make changes to copy.
If it is an array of objects, you may want to make a deep copy, i.e. making a copy of all the contained objects. But in your case, since Strings are immutable, a clone will suffice. However, considering storing your data in classes instead of strings.

Data structure to store 2D-arrays in JAVA

i am looking for a data structure to store two dimensional integer arrays.
Is List the rigth data structure or should i use another one?
Can someone give me a short example on how to create such a data structure and how to add a 2d array?
Edit: I want a data structure in which i want to store int[11][7] arrays.
For instance ten, int[11][7] arrays.
If you need to store a number of int[][] arrays in a data structure, I would probably recommend that you store the int[][] arrays in an Object that represents what the data contains, then store these Objects in an ArrayList.
For example, here is a simple Object wrapper for your int[][] arrays
public class 2DArray {
int[][] array;
public 2DArray(int[][] initialArray){
array = initialArray;
}
}
And here is how you would use them, and store them in an ArrayList
// create the list
ArrayList<2DArray> myList = new ArrayList<2DArray>();
// add the 2D arrays to the list
myList.add(new 2DArray(myArray1));
myList.add(new 2DArray(myArray2));
myList.add(new 2DArray(myArray3));
The reason for my suggestion is that your int[][] array must have some meaning to you. By storing this in an Object wrapper class, you can give it a meaning. For example, if the values were co-ordinates, you would call your class Coordinates instead of 2DArray. You, therefore, create a List of Coordinates, which has a lot more meaning than int[][][].
An array is not just an idea about how to store information, it is also an implementation of how to store data. Thus, if you use an array, you have already selected your data structure.
If you want to store data in a data structure, you need to concentrate on how the data structure is used, think about how you will retrieve data and store data, how often you do each operation, and how much data you will be working with. Then you know which methods must be optimum, and have an idea of whether the data can reside in memory, etc.
Just to give you an example of how many ways this could be solved:
You could flatten the array into a 1D array, and use x*num_columns+y as the index
You could create an Object to contain the pair, and put the array in a Map
You could use a linked list containing linked lists.
You could use a tree containing trees.
You could use a list containing trees.
You could create a partial order over the pair and then put all the elements into one tree.
All of these solutions depend heavily on which operations are more important to optimize. Sometime it is more important to update the data structure quickly, sometimes not. The deciding factor is really the rest of your program.
So you want to store a collection of 2D arrays: if the collection is fixed size add another dimension:
int[][][] arrColl
If the collection is variably sized, use your favorite implementation of Collection<int[][]> (ArrayList, LinkedList, etc.):
Collection<int[][]> arrColl
based on your edits :
List<Integer[][]> is what you need - this will allow you to add any numbers of 2D Integer arrays. Note that this will involve boxing and unboxing - something that should be avoided if possible.
If it suffices ( if you know how many 2D int arrays you need in advance ), you can even use int[][][] - a 3D array of ints - this does not involve boxing/unboxing.
If size is fixed, then use int[][] else List<List<Integer>>.

Categories

Resources