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.

kingdom
  • 41
  • 1
  • 7
  • 4
    Could you please show the exact declaration of you `ArrayList`? I got a suspicion that you either use `ArrayList` or a raw type... – Turing85 May 23 '17 at 17:41
  • Possible duplicate of [Working with a List of Lists in Java](https://stackoverflow.com/questions/1474954/working-with-a-list-of-lists-in-java) – KevinO May 23 '17 at 17:42
  • what are those elements for a type?? – ΦXocę 웃 Пepeúpa ツ May 23 '17 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](http://stackoverflow.com/help/mcve))? – Sean Van Gorder May 23 '17 at 17:48

4 Answers4

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.

gajju_15
  • 527
  • 4
  • 17
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

slal
  • 2,657
  • 18
  • 29
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);
GP007
  • 691
  • 2
  • 9
  • 24
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++;
    }

}

Ganesh
  • 1
  • 1
  • 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](/help/how-to-answer). – Community Mar 14 '23 at 01:12