Re: ArrayList of ArrayList?
Here's some code I wrote up in a couple seconds. I used Strings to show you an example without doing 100% of your homework. It's quite chivalrous of me to give you this much, but I'm assuming by your current knowledge that you're here to learn. so please do. :)
Code java:
import java.util.ArrayList;
public class Main {
private static ArrayList<ArrayList<String>> parent;
public static void main(String[] args)
{
parent = new ArrayList<ArrayList<String>>();
}
public static void addChild(String[] childText)
{
ArrayList<String> child = new ArrayList<String>();
for(String text : childText)
{
child.add(text);
}
parent.add(child);
}
public static void removeChild(int index)
{
if(index >= 0 && index < parent.size())
{
parent.remove(index);
}
}
}
Re: ArrayList of ArrayList?
You could always take a look at this: http://www.javaprogrammingforums.com...t-example.html.
It has samples of how to implement many common features of multi-dimensional ArrayLists (in that case, 2D).
Re: ArrayList of ArrayList?
Or I can get sniped by a mod. :) I should have used the search bar to look for that post.
Re: ArrayList of ArrayList?
Okay, that helps quite alot, I had already re-examined my code and had seen that I needed to change the structure of how I had everything set up, and this helps some of the pieces fall into place.
One other quick question though- I'm going to be implementing those functions with Object instead of String so that they can be filled up with whatever object that a user would want to do, but because of that I'm unsure how to put the syntax for that in my main called function, whenever I try to code a line such as
Code Java:
parent.addChild(/*This is where I'm having trouble knowing what to put in here to correctly add a child*/);
Any tips how to word this?
Re: ArrayList of ArrayList?
Quote:
Originally Posted by
ben456123
One other quick question though- I'm going to be implementing those functions with Object instead of String so that they can be filled up with whatever object that a user would want to do, but because of that I'm unsure how to put the syntax for that in my main called function, whenever I try to code a line such as
You should almost definitely be using Generics instead of simply dealing with Objects- that way the user can still populate your data structure with anything, but you still have some semblance of type safety.
Quote:
Originally Posted by
ben456123
Code Java:
parent.addChild(/*This is where I'm having trouble knowing what to put in here to correctly add a child*/);
Any tips how to word this?
Well, what do you want that method to take as a parameter? Another List? An array to be converted into a List? A single Object? Something else?