2

I have nested ArrayList that looks like that in Java:

myArrayList = [element1, element2, [element3]] 

I would like to add elements so the ArrayList will look like that:

myArraylist = [element1, element2, [element3, element4, element5]] 

I tried to use;

myArrayList.get(2).add(elemet4);
myArrayList.get(2).add(elemet5);

but as a result I got:

myArraylist = [element1, element2, [element3], element4, element5] 

Any hints how to resolve that will be much appreciated.

edit: My bad, I should have attached Java code and avoid misleading you guys. Anyway here it is:

ArrayList<ArrayList<String>> finalArray = new ArrayList<ArrayList<String>>();
ArrayList<String> finalArrayTempCopy = new ArrayList<String>();
ArrayList<Integer> transactionTemp = new ArrayList<Integer>();

private ArrayList<Integer> addTransaction(){

        System.out.println("Enter transaction amount:");
        int amount = scanner.nextInt();
        scanner.nextLine();
        transactionTemp.add(Integer.valueOf(amount)); 
        return transactionTemp;
}
private int searchName(String name){
        int indexImienia = customerName.indexOf(name);
        return indexImienia;
    }

public void sumUp(){
        String name = "Tom";
        String branch = "First";
        int indexImienia = searchName(name);
        String transactionsAsString = transactionTemp.toString();   
        finalArrayTemp.add(name);
        finalArrayTemp.add(branch);
        finalArrayTemp.add(transactionsAsString);
        finalArrayTempCopy = new ArrayList<String>(finalArrayTemp);

        finalArray.add(indexImienia, finalArrayTempCopy);
}

Later in the code if I want to add single transaction I use the following method

public void addSingleTransaction(){
int indexImienia = 0;
int amount =20;
finalArray.get(indexImienia).add(2, String.valueOf(amount));
}

Editing my post I realised that the problem might lie with converting ArrayList into string and adding it to finalArray as string. Anyway, I will be grateful for your insight.

4
  • 4
    Could you please show the exact declaration of you ArrayList? I got a suspicion that you either use ArrayList<Object> or a raw type... Commented May 23, 2017 at 17:41
  • Possible duplicate of Working with a List of Lists in Java Commented May 23, 2017 at 17:42
  • what are those elements for a type?? Commented May 23, 2017 at 17:45
  • The pseudocode you give should work, so there's probably a bug in your actual code. Can you make an MCVE (Minimal, Complete, Verifiable Example)? Commented May 23, 2017 at 17:48

4 Answers 4

2

i think you looking for

    List<Object> list1=new ArrayList<Object>();
    List<Object> list=new ArrayList<Object>();
    list.add("element1");
    list.add("element2");

    list1.add("element3");
    list.add(list1);

    List<Object> listobj=(List<Object>) list.get(2);

    listobj.add("element4");
    listobj.add("element5");
    System.out.println(list);

you are adding element to your parent list not your nested list, for which you have to store the reference of your nested list, then you can add other elements.

Sign up to request clarification or add additional context in comments.

3 Comments

For list variable, you can clone of list1.
I think cloning is not required.
I got requirement.
1

Seems like you are looking for:

 ArrayList<Object> myArrayList = new ArrayList<Object>();
 ArrayList<Integer> list1 = new ArrayList<Integer>;

 myArrayList.add(element1);
 myArrayList.add(list1);

Or

ArrayList<ArrayList<Integer>> myArrayList = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> list1 = new ArrayList<Integer>;
ArrayList<Integer> list2 = new ArrayList<Integer>;

list1.add(element1);
list2.add(element2);
list2.add(element3);

myArrayList.add(list1);
myArrayList.add(list2);

Assuming, element is an Integer

Comments

0

As per the psuedo code shared, There is some issue with your main array declaration.

ArrayList can only have one type of data in it. Therefore, your declaration of array can be like :

ArrayList<Element> arr = new ArrayList<Element>();  

or to store List in it:

ArrayList<ArrayList<Element>> ar2 = new ArrayList<ArrayList<Element>>();

If you wish to store both type of values in same structure you can use Object for storing any type of value, since it is the root of all classes. In this case your declaration will be something like:

     ArrayList<Object> arrOuter = new ArrayList<Object>();

     Element e1 = new Element();
     Element e2 = new Element();
     arrOuter.add(e1);
     arrOuter.add(e2);

     ArrayList<Element> arrNested  = new ArrayList<Element>();
     Element e3 = new Element();
     arrNested.add(e3);

     arrOuter.add(arrNested);

But in this case you can not directly call add method on data returned by arrOuter, since the data returned is of Object class. You have to explicitly cast it to list and then add another data.

 Element e4 = new Element(); 
 Element e5 = new Element();
 ((ArrayList) arrOuter.get(2)).add(e4);
 ((ArrayList) arrOuter.get(2)).add(e5);

Comments

0
List<List<Integer>> list=null;
public FirstGraph(int vertex) {
   list=new ArrayList<List<Integer>>(5);
    int i=0;
    while(i<vertex) {
        List<Integer> a = new ArrayList<Integer>();
        list.add(a);
        i++;
    }

}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.