0

Perhaps I'm not phrasing this correctly, but I have built a class that makes a dynamic array that doubles every time it reaches it's maximum from input. I am now trying to integrate that into another class that will make a list of strings using what I already built in the dynamic array class. Something like:

 public StringList() {
        DynamicArray2 StringList= new DynamicArray2();}

But I know that isn't right because then I can't refer to it for the rest of the class because it will be cut off. Any suggestions?

4
  • 1
    It's not clear to me what you're asking -- perhaps you can provide some more context, or an example showing what you're looking for? Be sure also to check the question checklist. Thanks! Commented Oct 31, 2013 at 3:12
  • use Arrays.copyOf to expand your array Commented Oct 31, 2013 at 3:12
  • 1
    I didn't understand either, explain more please. Commented Oct 31, 2013 at 3:13
  • I made a class that I called DynamicArray2 that creates a dynamic array. I am now trying to use this class in another class to create a list of strings. I want this new class to use the code I have in the DynamicArray2 class to create an array for this list... basically, use the DynamicArray2 class instead of rewriting all the code. I am trying to figure out how to create a new empty "StringList" object that uses the code from the DynamicArray2 class. Commented Oct 31, 2013 at 3:16

1 Answer 1

1

I assume I understand what you are saying. You are declaring a variable inside of a class's constructor which, as you said, makes it out of scope with the rest of the class. Try moving the declaration outside of the constructor.

public class StringList {

    DynamicArray2 stringList;        

    public StringList() {
        stringList= new DynamicArray2();
    }

}

Or maybe you just want to have a publicly accessible DynamicArray2 object? Try:

public DynamicArray2 stringList = new DynamicArray2();
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to have worked, but now I am trying to add a string at an index, like "stringList[i]=a", which does not seem to be working. Any ideas?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.