1

I have created a duplicate array using this code

List<Games> gameSortList = new ArrayList<Games>(gamesList);

I am very new to this concept and don't really know how it works. I am using this duplicate array list to sort the original without editing it. However the new array list does not allow me to use any of the gets and sets for the original array list. Am I even declaring it right ?

Here is the declaration of the duplicate

  private void BtnAscActionPerformed(java.awt.event.ActionEvent evt) {
if (evt.getActionCommand().equals("⇑")) {
 List<Games> gameSortList = new ArrayList<Games>(gamesList);
 int newSmall = 9999999 ;
 int smallIndex = 0;
 switch (RadSelection){
     case "index":
        while (gameSortList.size() != 0)  {
           for (int i = 0; i < gameSortList.size(); i++) {
            // if (gameSortList.)  {
              gameSortList.getDev();   
            // }
           }

       } 
         break;
     case "Meta":

         break;
     case "Personal":

         break;
 }
 }

 }  

.getdev is one of the methods used for the Games class which is used in gamesList

What im trying to go is this psudo code :

Make Duplicate array list
smallnum = 99999;
smallIndex = 0

 until ( GetSizeOfDuplicate = 0){

for ( i  > GetSizeOfDuplicate , i ++)

     If Duplicate.getRank < small num{

       smallnum = Duplicate.getRank

      smallIndex = i } 

print Duplicate.getRank(smallIndex )

delete Duplicate.getRank(smallIndex )

smallnum = 99999;

}loop
Dzyuv001
  • 318
  • 1
  • 6
  • 18
  • 2
    Your code example makes a shallow copy of gamesList and assigns it to gameSortList. I'm a little confused about not being allowed to "use the gets and sets for the original array list." Care to clarify? – MarsAtomic May 07 '15 at 00:26
  • I have made the array public class DataBaseGUI extends javax.swing.JFrame { `Games game;` `ArrayList gamesList = new ArrayList();` ` associated variables such as name genre etc..` – Dzyuv001 May 07 '15 at 00:33
  • When you try to use the gets and sets for the original array list it gives you an exception? a compilation error? – Shondeslitch May 07 '15 at 00:34
  • @MarsAtomic this is actually not a shallow copy, it's a deep copy as per the javadocs for [`ArrayList`'s constructor](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#ArrayList(java.util.Collection)), which may be the root of the OP's problem – Mitchell Carroll May 07 '15 at 00:36
  • @ Shondeslitch No errors for the original array list , however when i use them with the duplicate it say stuff like this method does not exist – Dzyuv001 May 07 '15 at 00:36
  • Please add new code to your answer by editing it. – MarsAtomic May 07 '15 at 00:37
  • @Mitchell Carroll im very for being such a full but what is the difference and which one do I need? – Dzyuv001 May 07 '15 at 00:39
  • @Dzyuv001 You're probably trying to use `ArrayList` methods on the `List` that you build, `ArrayList` extends `AbstractList`, which in turn implements the `List` interface, which is why you get no errors for this conversion. The reason the method does not exist is because `List` doesn't contain all of the methods `ArrayList` does, and the JRE doesn't know to look in `ArrayList`'s methods because the object is just simply a `List`. – Mitchell Carroll May 07 '15 at 00:40
  • 1
    @MitchellCarroll I'm not seeing the language in the Java Doc that indicates that he's making a deep copy. My comment was based on this SO [answer](http://stackoverflow.com/questions/6536094/java-arraylist-copy). – MarsAtomic May 07 '15 at 00:42
  • @Dzyuv001 Your original intent seems to be using a shallow copy, but both will work. If you get a working shallow copy, then you can just manipulate it like normal and trust that the original changed if you like. I prefer using a deep copy then overwriting the original, as this is less ambiguous in exactly what's happening. – Mitchell Carroll May 07 '15 at 00:43
  • @MarsAtomic The answer you link has them straight using `list1 = list2`, which is a shallow copy. The poster here is calling the `ArrayList` constructor with another `ArrayList` as the argument, which javadoc says "Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator." Which is the definition of a [deep copy](http://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy) – Mitchell Carroll May 07 '15 at 00:45
  • @MitchellCarroll Ah, of course. I read too fast. – MarsAtomic May 07 '15 at 00:54
  • @MitchellCarroll Can this duplicate be used to create something similar to the psudo code i made – Dzyuv001 May 07 '15 at 01:15
  • @Dzyuv001 The pseudocode is a bit sparse, but am I correct in gathering that you're using selection sort as your sorting algorithm? – Mitchell Carroll May 07 '15 at 01:50
  • @ Mitchell Carroll Yes you are correct , would this be viable ? – Dzyuv001 May 07 '15 at 01:52
  • @Dzyuv001 Viable? Yes. Efficient? Not really, probably not a problem unless your list is going to be a couple hundred elements though. However, unless you're required to implement your own sort algorithm (like for an assignment or something), you can use `Collections.sort()` to do the sorting for you, no need to worry about shallow/deep copy or anything else. – Mitchell Carroll May 07 '15 at 02:00
  • @Mitchell Carroll If i use `Collections.sort()` that will edit the original arraylist which i want to keep unchanged. Also it's a relatively small program that will only have about 20 record at one time so long sorting time will not be a problem. – Dzyuv001 May 07 '15 at 07:41
  • @Dzyuv001 Ah, I didn't know you wanted to preserve the original list as well, in that case using the constructor the way you are should be fine, just change the line `List gameSortList = new ArrayList(gamesList);` to `ArrayList gameSortList = new ArrayList(gamesList);`, there is no reason to try to switch away from an `ArrayList` in this application, and it should get rid of your errors as well. – Mitchell Carroll May 07 '15 at 22:12
  • @MitchellCarroll `list1 = list2` is not a shallow copy, it's an *assignment*. See the answer you linked again: "A shallow copy of a collection is a copy of the collection structure". `new ArrayList(gamesList)` is a shallow copy like @MarsAtomic says. – olovb May 08 '15 at 19:24

2 Answers2

0

No, you'll just have the same objects in the new list.

Remember that one object can have many references.

What happens is that the new list has copies of the references to the same objects that you have in the first list. When you use the methods of the first list you are manipulating the sequence of references in that particular list, not the contained objects per se.

[Edit: fixed sloppy wording.]

olovb
  • 2,164
  • 1
  • 17
  • 20
  • It depends on what the objects are whether it contains the references to them or not. Primitive types are _always_ passed by value unless you use their wrapping classes. If it does hold references, then the original objects are changed, it doesn't manipulate the references. That is an absurd claim to make and is _absolutely wrong_. – Mitchell Carroll May 08 '15 at 18:11
  • @MitchellCarroll So, how do you store primitives in a Java `ArrayList`? – olovb May 08 '15 at 18:33
  • @MitchellCarroll Also please clarify exactly what is an absurd claim. – olovb May 08 '15 at 19:04
  • You don't manipulate references. Variables don't _hold_ references, they _are_ the references. When you use a reference in java, it's like using the actual object itself. C++ is where you have to worry about referencing and making sure you dereference stuff before you use it. And there's no difference between storing primitives in an `ArrayList` vs storing objects, the only difference is in where the data is actually put, but the syntax for initialization and storage is pretty much identical. – Mitchell Carroll May 08 '15 at 20:45
  • @MitchellCarroll "A variable of a class type T can *hold* a null reference or a reference to an instance of class T or of any class that is a subclass of T." Refer to JLS §4.12.2. And as far as I know you cannot store primitives in an `ArrayList`. – olovb May 08 '15 at 21:03
  • Now that you mention it, `ArrayList` can't hold raw primitive types, but Java's autoboxing makes it seem like you can, as when you declare `ArrayList` it's silently converting it to `ArrayList`. You still can't manipulate references themselves, unless you do so on purpose via assignment (which isn't really manipulating them, but instead getting rid of an old reference and replacing it with a new one). As a sort of analogy, c++'s `.` operator and `->` operator both map to the `.` operator in Java (kinda, but the point is the only thing you do with references is swap them). – Mitchell Carroll May 08 '15 at 21:21
  • @MitchellCarroll My wording was bad, it's correct that you don't manipulate the actual references, what I meant was that you manipulate the sequence of references within the `ArrayList`. – olovb May 08 '15 at 21:26
0

The error you're getting is because you're trying to call ArrayList methods on an object of type List, even though you constructed it using the ArrayList constructor, it's not implicitly convertible back to an ArrayList. Simply change the line

List<Games> gameSortList = new ArrayList<Games>(gamesList); 

to

ArrayList<Games> gameSortList = new ArrayList<Games>(gamesList);
Mitchell Carroll
  • 479
  • 5
  • 13