Results 1 to 7 of 7
  1. #1
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Arraylist question

    for the add(i,e) method in an ArrayList it states:

    insert a new element e into S to have index i; an error condition occurs if i < 0 or i > size();

    Could someone explain to me what is wrong with having i >size()?

    thanks

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylist question

    Quote Originally Posted by stuckonjava View Post
    for the add(i,e) method in an ArrayList it states:

    insert a new element e into S to have index i; an error condition occurs if i < 0 or i > size();

    Could someone explain to me what is wrong with having i >size()?

    thanks
    Suppose your ArrayList has ten elements in it; so its size == 10; also suppose that you want to add something at position 100; 100 > 10 and what are the other ninety elements supposed to be?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    stuckonjava is offline Senior Member
    Join Date
    Jan 2012
    Posts
    151
    Rep Power
    3

    Default Re: Arraylist question

    Quote Originally Posted by JosAH View Post
    Suppose your ArrayList has ten elements in it; so its size == 10; also suppose that you want to add something at position 100; 100 > 10 and what are the other ninety elements supposed to be?

    kind regards,

    Jos
    Is there not allowed to be a gap in between? if not why not? Thanks

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Arraylist question

    Is there not allowed to be a gap in between? if not why not? Thanks
    Not if you're trying to access them by index!

    If you have 10 elements spread across 100 indexes, what is the size of the array?
    10
    But what is the highest index?
    99

    So, if you iterate from 0 to 9, you get any elements in those indexes, but what about the ones at indexes 10-99?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylist question

    Quote Originally Posted by stuckonjava View Post
    Is there not allowed to be a gap in between? if not why not? Thanks
    You can mimic that behaviour wth a Map<Integer, T>

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    CostaInf is offline Member
    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    Default Re: Arraylist question

    Salut a tous le monde, j'ai essayer de faire un petit code java, (je suis débutant) pour manipuler des ressources (Enseignants et Enseignement) la première classe Enseignant (j'ai travailler avec ArrayList) comme suit:
    import java.util.ArrayList;
    public class Enseignant {
    private String NomEn;
    private String NiveauEn;
    private String idEnseign;


    public Enseignant(String idEn,String Nom , String Niveau ){
    // TODO Auto-generated constructor stub
    this.idEnseign = idEn;
    this.NomEn = Nom;
    this.NiveauEn = Niveau;
    }

    public void setNom(String nom) {
    this.NomEn = nom;
    }
    public String getNom() {
    return NomEn;
    }

    public void setId(String Id) {
    this.idEnseign = Id;
    }
    public String getId() {
    return idEnseign;
    }

    public void setNiveau(String Niveau) {
    this.NiveauEn = Niveau;
    }
    public String getNiveau() {
    return NiveauEn;
    }

    public String toString() {
    return "Enseignant [ Id : " + idEnseign+" Nom : " + NomEn + " Prénom : " + PrenomEn + " Niveau : "+NiveauEn+"]\n";
    }
    et la classe Enseignement aussi comme celle là! ArrayList, en changeant que les attributs (IntituleModule, Unite çàd(le module est appartient à une unité fondamentale ou secondaire)
    et le problème qui se pose, je veux connaitre si je peux élaborer une List des Lists(ArrayList d'ArrayList): dans la classe ressources j'ai développer cette méthode qui permet d'ajouter une ressources(Enseignant ou bien Enseignement) :
    public static void AddRess(String D,ArrayList<Enseignant> E,ArrayList<Enseignement> EM, ArrayList<Classe> C, ArrayList<Salle> S){

    final Ressources RSS = new Ressources(D,E,EM,);
    R.add(RSS);
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stu
    EnsModels E = new EnsModels();
    EnsmModels Em=new EnsmModels();

    ENS = E.getEns();
    ENSM = Em.getEnsm();

    ArrayList<Ressources> R = new ArrayList<Ressources>();

    AddRess(DEP,ENS, ENSM);

    System.out.println("Ressources Humaines!");
    System.out.print(R);
    quelqu'un me donner un un coup de main! S.V.P , et merci tout d'abord !

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Arraylist question

    Hi CostaInf, welcome to the forums!

    Please start a new thread for your question rather than adding it on to this one. (You will probably get the most responses if you post in English if you can.)

Similar Threads

  1. ArrayList Question
    By kraigballa in forum New To Java
    Replies: 1
    Last Post: 04-18-2012, 08:28 AM
  2. ArrayList and .contains() question
    By Dark in forum New To Java
    Replies: 4
    Last Post: 07-08-2011, 09:34 PM
  3. Question about arraylist
    By Metastar in forum New To Java
    Replies: 7
    Last Post: 10-01-2010, 11:53 PM
  4. ArrayList question
    By spatel14 in forum New To Java
    Replies: 4
    Last Post: 07-07-2010, 10:02 PM
  5. arraylist question
    By lisa.lipsky in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-16-2009, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •