Not sure how to use protected method in arraylisy

Hi,
Im wondering how to use the removeRange method in java arraylist, its a protected method which returns void.
http://java.sun.com/j2se/1.3/docs/api/java/util/ArrayList.html#removeRange(int,%20int)
So if my class extends Arraylist i should be able to call the method but the compiler states that it still has protected access in arraylist. Does this mean im still overriding the method in arraylist ? A little explanation of whats happeneing would be appreciated as much as an answer
thanks

In this codefinal class myClass extends java.util.ArrayList {
    private ArrayList array = new ArrayList();
    public myClass(ArrayList ary){
        for(int i=0;i<7;i++) {
            array.add(ary.get(i));
        ary.removeRange(0,7)
}You are defining a class called myClass which extends ArrayList.
You have a member variable called array which is an ArrayList
You have a constructor which takes a parameter called ary which is an ArrayList
Since ary is an ArrayList, you cannot call it's removeRange() method unless myClass is in the java.util package. Do not put your class in the java.util package.
It seems like what you want to do is to move the items in one ArrayList to another ArrayList rather than copying them. I wrote a program to do this. Here it isimport java.util.*;
public class Test3 {
  public static void main(String[] args) {
    MyClass myClass = new MyClass();
    for (int i=0; i<5; i++) myClass.add("Item-"+i);
    System.out.println("------ myClass loaded -------");
    for (int i=0; i<myClass.size(); i++) System.out.println(myClass.get(i));
    MyClass newClass = new MyClass(myClass);
    System.out.println("------ newClass created -------");
    for (int i=0; i<newClass.size(); i++) System.out.println(newClass.get(i));
    System.out.println("------ myClass now contains -------");
    for (int i=0; i<myClass.size(); i++) System.out.println(myClass.get(i));
class MyClass extends java.util.ArrayList {
    public MyClass() {}
    public MyClass(MyClass ary){
        for(int i=0;i<ary.size();i++) add(ary.get(i));
        ary.removeRange(0,ary.size());
}You should notice now that I don't create an ArrayList anywhere. Everything is a MyClass. By the way, class names are normally capitalized, variable names are not. Hence this line
MyClass myClass = new MyClass();
In the code above I create an empty MyClass and then populate it with 5 items and then print that list. Then I create a new MyClass using the constructor which takes a MyClass parameter. This copies the items from the parameter list into the newly created MyClass (which is an ArrayList) and then removes the items from the MyClass passed as a parameter. Back in the main() method, I then print the contents of the two MyClass objects.
One thing which may be a little confusing is this line.
for(int i=0;i<ary.size();i++) add(ary.get(i));
the add() call doesn't refer to anything. What it really refers to is the newly created MyClass object which this constructor is building. This newly created object is the 'this' object. So the line above could be rewritten as
for(int i=0;i<ary.size();i++) this.add(ary.get(i));
Hopefully this helps a little. The problems you seem to be having are associated with object oriented concepts. You might try reading this
http://sepwww.stanford.edu/sep/josman/oop/oop1.htm

Similar Messages

  • HT1414 i lost all my data and contacts on my iphone, is any method to get them back into my phone , i think they all in my itune stored but not sure how to use it !!!

    hi ,i lost all my data and contacts on my iphone, is any chance to get them back into my phone , i think they all in my itune stored but not sure how to use it !!!
    plz replay if you know how resolve this problem thanks

    Hello evanfromvic
    If you have a back up in iTunes, you can restore from that back up. The article below will assist with getting that backup restored.
    iOS: How to back up and restore your content
    http://support.apple.com/kb/HT1766
    Thanks for using Apple Support Communities.
    Regards,
    -Norm G.

  • Can I use the same plan that I have for my iphone 4s with my new ipad? or do I have to buy a new data plan for my new ipad?  I would love to use my new ipad away from home but not sure how to use it?

    Can I use the same plan that I have for my iphone 4s with my new ipad? or do I have to buy a new data plan for my new ipad?  I would love to use my new ipad away from home but not sure how to use it?  I am on Telus mobility with my iphone 4s.

    See the answer on the same question you asked a few minutes ago.

  • How to use protected method in jsp code

    Could anyone tell me how to use protected method in jsp code ...
    I declare a Calendar class , and I want to use the isTimeSet method ,
    But if I write the code as follows ..
    ========================================================
    <%
    Calendar create_date = Calendar.getInstance();
    if (create_date.isTimeSet) System.out.println("true");
    %>
    ============================================================
    when I run this jsp , it appears the error wirtten "isTimeSet has protected access in java.util.Calendar"

    The only way to access a protected variable is to subclass.
    MyCalendar extends Calendar
    but I doubt you need to do this. If you only want to tell if a Calendar object has a time associated with it, try using
    cal.isSet( Calendar.HOUR );

  • I'm not sure how to use this site....but here goes, I have a samsung galaxy and it's fully charged but it will not turn on, can someone give me some solutions as to how I can get it to work?

    I'm not sure how to use this site....but here goes, I have a samsung galaxy and it's fully charged but it won't turn on, can someone give me some solutions I can use to fix this problem???

    Try this website. Yes its sprint but it might help you. http://support.sprint.com/support/article/Troubleshoot_issues_related_to_your_Sa msung_Galaxy_S_II_not_turning_on_completely/WTroubleshootingGuide_542_GKB51165-T rend?INTNAV=SU:DP:OV:TSIS:SamsungGalaxySIi-AsYouGo:TroubleshootIssuesRelatedToYo urSamsungG

  • I need to program a Hittite Fractional​-N Synthesize​r Evaluation Kit with a HMC702LP6C​E an external YIG oscillator​. Not sure how to use Labview to control the PLL.

    I need to program a Hittite Fractional-N Synthesizer Evaluation Kit with a HMC702LP6CE an external YIG oscillator.  Not sure how to use Labview to control the PLL.

    Here is how to use the PLL. But I don't know of how to interact with that device
    http://zone.ni.com/devzone/cda/tut/p/id/3781
    And for thouse who don't know what a PLL is a free bonus link is here:
    http://digital.ni.com/public.nsf/allkb/07BC8D77D4E​9AE258625708B007CE74F?OpenDocument
    and a second one on what that device is: http://www.hittite.com/products/view.html/view/HMC​702LP6CE
    Now we are all caught up to speed.
    Sam S
    Applications Engineer
    National Instruments

  • How to use protected method of a class in application

    Hi,
      will u please tell me how to use protected method of class in application. (class:cl_gui_textcontrol, method:limit_text)
    Thanks in advance,
    Praba.

    Hi Prabha,
    You can set the maximum number of characters in a textedit control in the CREATE OBJECT statement itself.
    Just see the first parameter in the method . I mean MAX_NUMBER_CHARS. Just set that value to the required number.
    Eg:
    data: edit type ref to CL_GUI_TEXTEDIT.
    create object edit
      exporting
        MAX_NUMBER_CHARS       = 10
       STYLE                  = 0
       WORDWRAP_MODE          = WORDWRAP_AT_WINDOWBORDER
       WORDWRAP_POSITION      = -1
       WORDWRAP_TO_LINEBREAK_MODE = FALSE
       FILEDROP_MODE          = DROPFILE_EVENT_OFF
        parent                 = OBJ_CUSTOM_CONTAINER
       LIFETIME               =
       NAME                   =
      EXCEPTIONS
        ERROR_CNTL_CREATE      = 1
        ERROR_CNTL_INIT        = 2
        ERROR_CNTL_LINK        = 3
        ERROR_DP_CREATE        = 4
        GUI_TYPE_NOT_SUPPORTED = 5
        others                 = 6
    IF sy-subrc <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
               WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    In this case, the max: number of characters will be set to 10.
    I hope your query is solved.
    Regards,
    SP.

  • Not sure how to use IPC here

    Lets say X is a shell command that will get you into X-shell upon execution. There's a command, a module, Y that can be run only in X-shell. How can I run Y from a C++ code? I need to somehow tell my app that the environment it'll be dealing with will be X-shell and not the Linux shell. I haven't dealt with a case like that before and don't know how to incorporate IPC here. Would I take X-shell as the parent process and the spawned child as module Y? Not sure... I should point out that it's not possible to do something like "X-shell -c Y"; first X-shell has to be executed. Once in the X-shell environment, now Y can be called.
    A word of wisdom is appreciated.

    Thanks. I'm a very basic Linux user and have done most of my apps in Windows... Could you please explain a bit more -in a more beginner/intermediate level- on how to achieve that (in C++)? I'm somewhat familiar with getenv and setenv etc but not sure how to implement your suggestion using them.

  • How to use protected method of a particular class described in API

    I read in some forum that for accessing protected method reflcetion should be used. If I'm not wrong then how can I use reflection to access such methods and if reflection isn't the only way then what is another alternative. Please help me...
    regards,
    Jay

    I read in some forum that for accessing protected
    method reflcetion should be used. If I'm not wrong
    then how can I use reflection to access such methods
    and if reflection isn't the only way then what is
    another alternative. Please help me...Two ways:
    - either extend that class
    - or don't use it at all
    If you use reflection, you're very likely to break something. And not only the design. Remember that the method is supposed to be inaccessible for outside classes.

  • New Cisco IP SPA 303 - not sure how to use

    I received this phone as a gift (long story) but since its an IP phone, I am not sure I have a use for it. I have comcast triple play, and use the old style phone to connect to the jack on the comcast cable modem. However I would like to take advantage of what seems to be alot more features on this IP phone, but it didn't take long to realize just plugging the SW port into my router would do anything outside of assign the phone an IP address.
    Can someone spare me the thought of disappointing someone and having to return this phone and tell me how I can make use of it? Its specifially for my home office. For example, do I have to subscribe to a VOIP provider and inherit yet another gadget adapter for my desktop? I looked at vonage and the device they use seems to be the same technology as comcast with the rj45 jack.
    Any help appreciated.

    I'm not sure about connecting it via Comcast Triple Play, but you can use it via Voip, and there are some well recommended inexpensive voip services. Usomo for example, have monthly rates from £2.99 for a basic service, whereas others such as VoipTalk have a business package of £29 if you wanted to use the 303 as a switchboard but my first guess is that you're looking to use it as a single user phone?

  • Not sure how to use Movie Maker with my HP TrueVision HD webcam

    I've never used Movie Maker before. When I fire it up and double-click Webcam video in the ribbon I get a splash screen saying "Please Start YouCam"
    I assume it is possible to make a video using the build-in webcam, and to save it to disk.
    I need some help understanding how to do that.
    Thanks.
    Roricka

    Peer One,
    You know, if you hadn't replied, other people who might be inclined to actually help, might see that there were "0" replies, but now someone may actually think I've received helpful advice and skip it.  If that seems harsh, would you mind taking the time to address a couple of meta-issues?
    1. You state that Movie Maker is a Microsoft product. Now, my question was how to make Movie Maker work with the built-in camera on my HP laptop. Movie Maker itself seems to launch just fine, but it appears to be unaware of a piece of HP hardware, hence my post to an HP forum. How do you figure it is helpful to inform me (as if I didn't know) that Movie Maker is made by Microsoft? Does it matter if movie maker is made by Acme Software? My problem is essentially that I can't get Movie Maker to see my HP camera. Are you just attempting to scold me for not realizing Movie Maker is not an HP product? Is the forum ONLY to assist people having difficulty with HP software?
    2. You write "you might want to read up the instructions." Are you just being passive-aggressive or is this comment actually intended as assistance? Now, I fully expect you to double-down at this point. I know that almost all respondents to forums who make these kinds of remarks never apologize and admit they are simply being critical with no attempt to be helpful. Unless you believe that by stating the obvious, it is helpful. (Do you?) While it is of little value to explain this, let me say that the Microsoft instructions for Movie Maker that I was able to find (note: MM itself does not include a help entry on any of its ribbon menus or sub-menus -- and if you are unaware of that, don't you think you should have investigated it prior to taking on the role of scolder-in-chief for this thread?) did not explain how to activate one's built-in camera. I firmly believe such a help topic is covered somewhere, but I simply couldn't find it after considerable searching. I didn't feel it necessary to point that out in my question -- I guess I just assume people are more charitable or willing to help than was the ONE PERSON WHO COMMENTED AND THUS OFFERED HIS "1" UNDER THE "REPLIES" COLUMN.
    3. You ask if my laptop has a built-in camera. You know something? I know my four sentence post was a trifle difficult for some folks to read and understand (OK, now I plead guilty to a charge of passive-agressiveness), but don't you feel the use of the phrase "built-in webcam" (ok, ok, so I misspelled it "build-in" instead of "built-in") should have been a pretty strong clue that my laptop has a webcam?
    Well, I wish there was some way to totally delete your extremely condescending and unhelpful response. But maybe someone who actually can offer some assistance will ignore the "1" and make it a "2" for the number of replies.
    Thanks for your attention.
    roricka

  • HT1218 I'm not sure how to use this?

    Can someone help me understand how to use this

    Take a look at this link, http://support.apple.com/kb/ht1218?viewlocale=de_de

  • Arranging objects on a JPanel (Not sure how to use Layouts)

    Hey guys,
    It was suggested to me to use layouts to arrange Jbuttons fields and such. I tryed following the Java tutorial on the topic but I can't seem to follow it. If someone might look at my code and give me some pointers to arrange it. I'm not too familiar with the arrangement of objects, and what I do know is from BlueJ. This is is my code. Thanks in advance.
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    public class SonnetTest {
        public static void main(String[] args) {
             JFrame frame = new JFrame();
             frame.setResizable(false);
             //Creates a new PoemGenerator object
             final PoemGenerator vanorden = new PoemGenerator();
             //Sets the standard width of the fields
             final int FIELD_WIDTH = 20;
             //Initializes the input fields for author and title
             final JTextField authorField = new JTextField(FIELD_WIDTH);
             final JTextField titleField = new JTextField(FIELD_WIDTH);
             //Labels the input fields
             final JLabel titleLabel = new JLabel("Title");
             final JLabel authorLabel = new JLabel("Author");
             //Initializes the display area
             final JTextArea display = new JTextArea();
             display.setText(vanorden.verse);
             display.setEditable (false);
              //Initializes the submit and new poem buttons
             JButton submitButton = new JButton("Submit");
             JButton newPoemButton = new JButton("New Poem");
             //Constructs the panel      
             JPanel panel = new JPanel();
             panel.add(display);
             panel.add(authorLabel);
             panel.add(authorField);
             panel.add(titleLabel);
             panel.add(titleField);
             panel.add(submitButton);
             panel.add(newPoemButton);
             frame.add(panel);
             submitButton.setSize(5000,50);
             //Creates a listener to be used when the submit button is pressed
             class CheckAnswerListener implements ActionListener{
                  public void actionPerformed(ActionEvent event){
                       String authorGuess = authorField.getText();
                       //Compares the input with the correct (ignoring case)
                       if(authorGuess.compareToIgnoreCase(vanorden.Poet) == 0){
                            display.setText("Correct!");
                       else{
                            display.setText("Incorrect, the poet's name is " + vanorden.Poet + ".");
             ActionListener listener = new CheckAnswerListener();
             submitButton.addActionListener(listener);
             //Creates a listener to be used when the new poem button is pressed
             class NewPoemListener implements ActionListener{
                  public void actionPerformed(ActionEvent event){
                       PoemGenerator vanorden = new PoemGenerator();
                       display.setText(vanorden.verse);     
             ActionListener listener2 = new NewPoemListener();
             newPoemButton.addActionListener(listener2);
             //Sets the panel's size
             frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
        //Sets the variables to be used as dimensions for the window
        private static final int FRAME_WIDTH = 500;  //Good width for input fields
        private static final int FRAME_HEIGHT = 100;
    }

    Thank you. I put each of the components into its on panel, just for the sake of experimentation. The thing is though that only one panel shows up. How do I arrange them? I keep getting the error:
    cannot find symbol method setLayout(java.awt.GridLayout)
    Here is my revised code. Thanks for putting up with me.
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.JTextArea;
    public class SonnetTest {
        public static void main(String[] args) {
             JFrame frame = new JFrame();
             frame.setResizable(false);
             //Creates a new PoemGenerator object
             final PoemGenerator vanorden = new PoemGenerator();
             //Sets the standard width of the fields
             final int FIELD_WIDTH = 20;
             //Initializes the input fields for author and title
             final JTextField authorField = new JTextField(FIELD_WIDTH);
             final JTextField titleField = new JTextField(FIELD_WIDTH);
             //Labels the input fields
             final JLabel titleLabel = new JLabel("Title");
             final JLabel authorLabel = new JLabel("Author");
             //Initializes the display area
             final JTextArea display = new JTextArea();
             display.setText(vanorden.verse);
             display.setEditable (false);
              //Initializes the submit and new poem buttons
             JButton submitButton = new JButton("Submit");
             JButton newPoemButton = new JButton("New Poem");
             //Constructs the panel      
             JPanel panel = new JPanel();
             panel.add(display);
             JPanel panel2 = new JPanel();
             panel2.add(authorLabel);
             JPanel panel3 = new JPanel();
             panel3.add(authorField);
             JPanel panel4 = new JPanel();
             panel4.add(titleLabel);
             JPanel panel5 = new JPanel();
             panel5.add(titleField);
             JPanel panel6 = new JPanel();
             panel6.add(submitButton);
             JPanel panel7 = new JPanel();
             panel7.add(newPoemButton);
             frame.add(panel);
             frame.add(panel2);
             frame.add(panel3);
             frame.add(panel4);
             frame.add(panel5);
             frame.add(panel6);
             frame.add(panel7);
             submitButton.setSize(5000,50);
             //Creates a listener to be used when the submit button is pressed
             class CheckAnswerListener implements ActionListener{
                  public void actionPerformed(ActionEvent event){
                       String authorGuess = authorField.getText();
                       //Compares the input with the correct (ignoring case)
                       if(authorGuess.compareToIgnoreCase(vanorden.Poet) == 0){
                            display.setText("Correct!");
                       else{
                            display.setText("Incorrect, the poet's name is " + vanorden.Poet + ".");
             ActionListener listener = new CheckAnswerListener();
             submitButton.addActionListener(listener);
             //Creates a listener to be used when the new poem button is pressed
             class NewPoemListener implements ActionListener{
                  public void actionPerformed(ActionEvent event){
                       PoemGenerator vanorden = new PoemGenerator();
                       display.setText(vanorden.verse);     
             ActionListener listener2 = new NewPoemListener();
             newPoemButton.addActionListener(listener2);
             //Sets the panel's size
             frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.setVisible(true);
        //Sets the variables to be used as dimensions for the window
        private static final int FRAME_WIDTH = 260;  //Good width for input fields
        private static final int FRAME_HEIGHT = 200;
    }

  • External Hard Drive - Not Sure How to Use It

    Hi.
    My MacBook Pro's main hard drive is pretty much full. I just bought an external hard drive.
    Is there a way I can (in addition to backing up my most important files) somehow transfer certain files from the computer to the external hard drive (to free up space on the internal hard drive) and be able to use those files directly from the external hard drive whenever I need them?
    Is there a certain way I should set up the hard drive to do this?
    What kind of files should I do this with? Music? Movies? Pictures?
    If it's even possible, or wise to do this, how can it be done?

    It's safe to move any or all of the files that you have in the following folders:
    /Home/Documents/
    /Home/Downloads/
    /Home/Movies/
    /Home/Music/
    /Home/Pictures/
    /Home/Sites/
    You can copy the entire folders to your external drive. Then delete them from your main hard drive to free up space. Next create an alias for each of the folders, place the aliases in the main /Home/ folder, and rename them to the exact same names. The aliases will enable all other software to access the real folders on the external drive while taking up far less space on the main drive.
    Do not do the above with the /Home/Library/ folder because the alias trick does not work with that folder.
    If you aren't able to free up enough space doing the above, then replace the main hard drive with a larger one. This is actually the preferable solution rather than using the external hard drive.

  • Not sure how to use this icloud

    How do I register my PC with the same Appleid, as my iPad2?

    If you are trying to say that when you connect to iTunes on your computer your Device is not appearing.... Then...
    See Here  >  http://support.apple.com/kb/HT1808
    You may need to try this More than Once...
    Be sure to Follow ALL the Steps...
    After the successful Recovery...
    Restore from Backup
    http://support.apple.com/kb/ht1766

Maybe you are looking for