Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

Opinion needed - Array List or Linked List

So basically I need to make a program which stores cd information and I need to write it to file.I need to be able to:

Add,
Edit or
Delete the information

So I used the Array List but when written to file the contents are a little shoddy.

I need your opinion will it be better to stay with the array list or maybe go with the linked list?
Guessing from your requirements,it would be better to implement your code through a LinkedList.

http://chaoticjava.com/posts/linkedlist-vs-arraylist/
(Jun 30, 2012 10:32 AM)anuvab1911 Wrote:  Guessing from your requirements,it would be better to implement your code through a LinkedList.

http://chaoticjava.com/posts/linkedlist-vs-arraylist/

Thanks for your input🤝. . .I was hoping to stay with the array list but I guess I should just stop being lazy
Wait, How are you storing the cd information? Is this a String ArrayList?
(Jun 30, 2012 10:53 AM)node.js Wrote:  Wait, How are you storing the cd information? Is this a String ArrayList?

Sorry I mention this, yes it is a String Array list
(Jun 30, 2012 10:54 AM)starvinmarvin1 Wrote:  Sorry I mention this, yes it is a String Array list

See that's the problem. Do something like this:

Code
import java.util.ArrayList;

public class CDManager {

    public static void main(String[] args) {
        ArrayList<CD> cds = new ArrayList<CD>();

        CD cd1 = new CD();
        cd1.artist = "SoFlawLess_";
        cd1.name = "Best Hits of SoFlawLess_";
        cds.add(cd1);

        CD cd2 = new CD();
        cd2.artist = "starvinmarvin1";
        cd2.name = "Awesome Hits of starvinmarvin1";
        cds.add(cd2);

        for (CD cd : cds) {
            System.out.println("CD Name: " + cd.name);
            System.out.println("CD Artist: " + cd.artist);
            System.out.println();
        }
    }
}

Code
public class CD {

    public String artist;
    public String name;

}
(Jun 30, 2012 11:02 AM)node.js Wrote:  See that's the problem. Do something like this:

Code
import java.util.ArrayList;

public class CDManager {

    public static void main(String[] args) {
        ArrayList<CD> cds = new ArrayList<CD>();

        CD cd1 = new CD();
        cd1.artist = "SoFlawLess_";
        cd1.name = "Best Hits of SoFlawLess_";
        cds.add(cd1);

        CD cd2 = new CD();
        cd2.artist = "starvinmarvin1";
        cd2.name = "Awesome Hits of starvinmarvin1";
        cds.add(cd2);

        for (CD cd : cds) {
            System.out.println("CD Name: " + cd.name);
            System.out.println("CD Artist: " + cd.artist);
            System.out.println();
        }
    }
}

Code
public class CD {

    public String artist;
    public String name;

}

You make Java seem easy bro. . .but how am I going to take lots of input ?

Thanks for your help🤝
Well,to take in lots of input,create a class CD like this as suggested by SoFlawLess_:

Code
public class CD{
  public String <info_field_0>;
  public String <info_field_1>;
  public String <info_field_2>;
  public CD(String i0,String i1,String i2){
  <info_field_0>=i0;
  <info_field_1>=i1;
  <info_field_2>=i2;
  }
}

Then create an ArrayList or LinkedList in your main class by the name say,cdList by writing it like:

Code
ArrayList<CD> cdList=new ArrayList<CD>();

Then,the function to add to the list will be something like:
Code
public static insertToList(String artist,String name,String date){
  CD cdNew=new CD(artist,name,date);
  cdList.add(cdNew);
}
Thanks for your help guys.

Got it sorted
(Jun 30, 2012 11:02 AM)node.js Wrote:  See that's the problem. Do something like this:

Code
import java.util.ArrayList;

public class CDManager {

    public static void main(String[] args) {
        ArrayList<CD> cds = new ArrayList<CD>();

        CD cd1 = new CD();
        cd1.artist = "SoFlawLess_";
        cd1.name = "Best Hits of SoFlawLess_";
        cds.add(cd1);

        CD cd2 = new CD();
        cd2.artist = "starvinmarvin1";
        cd2.name = "Awesome Hits of starvinmarvin1";
        cds.add(cd2);

        for (CD cd : cds) {
            System.out.println("CD Name: " + cd.name);
            System.out.println("CD Artist: " + cd.artist);
            System.out.println();
        }
    }
}

Code
public class CD {

    public String artist;
    public String name;

}
Reveal the behavior through an interface (for example, List<?>) rather than implementation (ArrayList<?>)

Also, I believe an array list is the better choice.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by gurlhackerclubMay 19, 2020 05:18 PM 8 260 May 19, 2020 05:18 PM
Last Post: k1r3n
[Java] by Juice.Mar 10, 2020 07:02 PM 0 136 Mar 10, 2020 07:02 PM
Last Post: Juice.
[C] by g4143Dec 28, 2019 09:24 AM 1 351 Dec 28, 2019 09:24 AM
Last Post: an balker