creating arrayList of arraylists [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was trying to create an ArrayList of ArrayLits. Then, whenever I added an ArrayList to my initial ArrayList, I get My 2nd ArrayList in the initial ArrayList copied to the first ArrayList in the initial ArrayList. Here is my Code. Thank you for any help.
ArrayList<ArrayList> msebi = new ArrayList<ArrayList>();
ArrayList<Pair> temp = new ArrayList<Pair>();
Stack<ArrayList> arrayListStack = new Stack<ArrayList>();
Pair pair = null;
for(int i=1;i<finalList.size()-1;i++){
pair = finalList.get(i);
if(finalList.get(i+1).getDepth() > finalList.get(i).getDepth()){
temp.add(pair);
if(i == finalList.size()-2){
temp.add(finalList.get(i+1));
msebi.add(temp);
}
else {
temp.add(pair);
msebi.add(temp);
temp.clear();
}
}
Kindly Note that I have created another Class that is Named Pair. This Pair consist of a String and depth, and thus we have getDepth(); Also, I have in the main class ArrayList finalList = new ArrayList();
Thus, Whenever I am trying to copy the content of the ArrayLists within the msebi ArrayList, I got the values of the second arraylist the same as the first one.

When you add your ArrayList called temp to your outer ArrayList called msebi, msebi contains a reference to temp, not a copy. Then you immediately clear() temp and build it up again. You have multiple references to the same temp list in msebi.
Create a new ArrayList instead of clearing the old one. This way, when a list is added, it's left alone, because you will be operating on another list object.

Related

How to create and fill a list of arrays in java? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have the field "listofRows":
private List<String[]> listOfRows;
This list is supposed to hold several string arrays.
I then read in a line from a csv file, split the line into separate parts (using the commas as separators) and save the resulting strings in a string array:
String[] stringArray = line.split(",");
According to the debugger, the array stringArray now holds the two words from the CSV-file ("test" and "one").
The problem is with the next line of code:
listOfRows.add(stringArray);
This line does not work.
I can't add the array to the list.
How can I add an array to an arraylist?
NB: I know how I could add the elements of an array to a list separately. This question has already been answered on StackOverflow.
However, I want to add the array as a whole (!).
That is, every element of the list is supposed to be an array.
You need to instantiate your list:
private List<String[]> listOfRows = new ArrayList<String[]>();
In constructor initialize your list and return unmodified list to outside
public CSVReader(){
this.br = null;
this.line = "";
this.splitSign = ",";
listOfRows =new ArrayList<>();
}
Initialize the list or else you will get null pointer exception
public CSVReader(){
this.br = null;
this.line = "";
this.splitSign = ",";
listOfRows =new ArrayList<>(); //Initialization
}
You need to initialize your List and then add the string array to it.
private List<String[]> listOfRows = new List<String[]>();
ListOfRows.add(stringArray);
This would work without throwing any error such as Null pointer: not initialized.

Removing item from generic list java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I need to remove an item from a generic list in java, but I don't know how to do this. If it was a list of int, I would just set it to zero, if it was strings I would set it to null. How can I do this with a generic list, and I can't use an methods of Arraylist or anything like that, I have to write the method myself.
You can remove an individual object instance with List.remove(Object) or you can remove a specific instance from a specific index with List.remove(int). You can also call Iterator.remove() while you iterate the List. So, for example, to remove every item from a List you could do
Iterator<?> iter = list.iterator();
while (iter.hasNext()) {
iter.remove();
}
I would think that if you are implementing a list yourself you should move all elements after the element you are deleting down one position, set the last one to null, and if you are keeping track of the size of your list, reduce this by one. Something like this
public Object remove(int remove_index){
Object temp = list[remove_index];
for(int i=remove_index;i<size-1;i++){
list[i] = list[i+1];
}
list[--size] = null;
return temp;
}
static <T> List<T> remove(List<? extends T> inputList, int removeIndex)
{
List<T> result = new ArrayList<T>( inputList.size() - 1 );
for (int i = 0 ; i < inputList.size() ; i++)
{
if ( i != removeIndex )
{
result.add( inputList.get(i) );
}
}
return result;
}

cannot convert from ArrayList<Integer> to ArrayList<String> [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a ArrayList with integer value and I want to read this value from another ArrayList consist string value. How can I do that ?
public static ArrayList<Integer> scorerValue = new ArrayList<Integer>();
and I want read here using
ArrayList<String> score_Number ;
Okay. So let's first declare our List<String>..
List<String> score_Number = new ArrayList<String>();
// Remember you program to an interface. Not a concrete implementation.
Then to convert you..
// Do some magic.
That's right Magic. Magic involving the toString() method. You will need to call toString() in a for each loop. Finally, you will need to use the List.add() method, to add the newly created String values to your list score_Number.
NOTE: Follow the naming conventions. score_Number should be scoreNumber.
Try,
ArrayList<Integer> integerList = new ArrayList<Integer>();
// add Integer Elements
ArrayList<String> stringList = new ArrayList<String>();
for(Integer i : integerList){
stringList.add(i+"");
}
try this:
ArrayList<Integer> scorerValue = new ArrayList<Integer>();
scorerValue.add(1);
ArrayList<String> score_Number = new ArrayList<String>();
for (Integer i : scorerValue)
score_Number.add(i.toString());
System.out.println(score_Number);

Have complication with ArrayList of ArrayList [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am implementing an algorithm,for that I need to use ArrayList of ArrayList.I want to retrieve each ArrayList from the Main ArrayList.
Eg: If the Main ArrayList contains 5 ArrayList and Each ArrayList in the main list contains 5 rows.
MainArrayList:
[1,2,3,4,5,a , 2,3,4,5,3,b , 7,8,9,4,5,d] //1st Arraylist
[1,2,3,4,5,e , 2,3,4,5,3,n , 7,8,9,4,5,e] //11nd Arraylist
[1,2,3,4,5,f , 2,3,4,5,3,t , 7,8,9,4,5,q] //111rd Arraylist
[1,2,3,4,5,c , 2,3,4,5,3,b , 7,8,9,4,5,a] //1vth Arraylist
[1,2,3,4,5,r , 2,3,4,5,3,m , 7,8,9,4,5,z] //v th Arraylist
I need to access each ArrayList from the main list and print it's content row by row
i.e for example
print 1st ArrayList from the main list.
1,2,3,4,5,a
2,3,4,5,3,b
7,8,9,4,5,d
similarly I need to print all arraylist in the main list same as above.
you can do it using ArrayList
List<List<Object>> twoDimArray = new ArrayList<ArrayList<>>();
twoDimArray.add(Arrays.asList("1","2","3");
for(List<Object> list: twoDimArray){
for(Object o : list){
System.out.println(o.toString());
}
}
And of course using HashMap:
Map<String,List<Object>> map = new HashMap<String,List<Object>>();
for (Map.Entry<String, String> entry : map.entrySet())
{
System.out.println(entry.getKey() + "/" + entry.getValue());
}
Use HashMap for resolving this problem.

Retrieve ArrayLIst which is a value in HashMap [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a HashMap HashMap<Integer, ArrayList<Bundle>> hashMap I am able to put values in the HashMap . And the hashmap is passed to the next class . Now how to get the ArrayList of Bundle from HashMap. Any how I am using Iterator Set . Still I am not able to list the values of HashMap which is an arraylist. This is actually am Message picker . And I have multiselection mode on using callback for listview. When I click on a sms list , I need to get some details and store it in a hashmap . There might be chance that a sms can have multiple recepinets. The key will be position . I need to take all the values of the HashMap that is ArrayList into one single ArrayList. Say int the 0th position we have [0,[A,B]] ,[1,[C,D]] , i want to create a new arraylist and store A , B, C, D in it.
Here you have a complete example:
public class Test {
private final String value;
public Test(String value) {
this.value = value;
}
public static void main(String[] args) {
HashMap<Integer, ArrayList<Test>> example = new HashMap<Integer, ArrayList<Test>>();
ArrayList<Test> test1 = new ArrayList<Test>();
test1.add(new Test("HELLO"));
ArrayList<Test> test2 = new ArrayList<Test>();
test2.add(new Test("HELLO2"));
example.put(1, test1);
example.put(2, test2);
// We get the second arraylist
// Where 2 is the Integer we added in example.put(2, test2);
ArrayList<Test> testExtracted = example.get(2);
System.out.println(testExtracted.get(0).value); // Prints HELLO2
}
}
you can try like that
ArrayList<Bundle> value = hashMap.get(yourkey);

Categories

Resources