Add an element in arraylist of arraylist using index - java

I have a arrayList done this way:
ArrayList<ArrayList<Double>> level = new ArrayList<ArrayList<Double>>(levels + 1);
So something like:
enter image description here
What I would do is add a new element to the internal list saved in a certain index i of the external list.
How can I do this?
I can't use method arrayList.add(i, 0.0);.

Use index from the external ArrayList and use it to add the number. Like this
level.get(i).add(1.1);

You need to get the list at a given position first and then insert into it the value desired.
Example:
final List<List<Double>> listOnList = new ArrayList<List<Double>>();
listOnList.add(new ArrayList<Double>());
listOnList.get(0).add(155.0);

Related

Best way to store data in array

I get from database some informations and I'd like to store it like:
Person[0] {name:"Marie", email:"marie#marie.com", adress:"address marie"}
Person[1] {name:"Josh", email:"josh#josh.com", adress:"address josh"}
...
So I can add more items, access items using position and after user show each position, remove it from array. eg: after user see array position 0 (Marie) info, remove array position 0 from memory.
What is the best way to do it? array, arraylist, arraymap...? How to declare, add and remove positions infos?
Thanks.
I guess you can use LinkedList may be, it provides constant O(1) time for adding item at last and removing first item.
You can use offer or add methods to add item and poll() to remove first item
You can do below operations
val list = LinkedList<String>()
list.add("Android") // add items to the end
list.add(5, "Hello") //adds item at mentioned position
list.poll() // removes first item
list.removeAt(4) //removes item at certain position
list.pollLast() // removes last item
To achieve what you are expecting, you could do something like this:
List<Map<String,String>> data = new ArrayList();
Map<String,String> map = new HashMap();
map.put("name","Marie");
map.put("email","marie#marie.com");
map.put("adress","address marie");
data.add(map);
System.out.println(data.get(0));
For inserting multiple items:
List<Map<String,String>> data = new ArrayList();
Map<String,String> map = new HashMap();
for(Class a : object)
{
map.put("name",a.name); //a.name is just from my imagination only. use your own aproach to get name.
map.put("email",a.email);
map.put("address",a.address);
data.add(map);
}
System.out.println(data.get(0));
Use ArrayList instead of Array for store data
String[] Person = {name:"Marie", email:"marie#marie.com", adress:"address marie"}
Person[1] {name:"Josh", email:"josh#josh.com", adress:"address josh"}
ArrayList<String> data = new ArrayList()<String>;
data.add(Person);

Getting ArrayList Value Inside An ArrayList - Java

I have a problem with pulling out a value from an Arraylist inside an Arraylist.
I'll just give an example of my problem.
Example:
ArrayList alA = new ArrayList();
ArrayList alB = new ArrayList();
alA.add("1");
alA.add("2");
alA.add("3");
alB.add(alA);
System.out.println(alB.get(0));
This will return [1, 2, 3] as the result.
In my case I only need to print out 3. How do I achieve this?
Just call get on the inner array:
System.out.println(((List) alB.get(0)).get(2));
Note that by using generics, you'll eliminate the need to cast:
List<String> alA = new ArrayList<>();
List<List<String>> alB = new ArrayList<>();
alA.add("1");
alA.add("2");
alA.add("3");
alB.add(alA);
System.out.println(alB.get(0).get(2));
Simply do the following if you don't want to change your other portions of current code
System.out.println(((ArrayList)alB.get(0)).get(2));
System.out.println(alB.get(0)); return alB's 0th index element which is alA. Since you want the element 3, you need to get the 2nd index element of alA, in this case it is alA.get(2);
Combined:
System.out.println(((ArrayList)alB.get(0)).get(2));

Getting instance of class X from an ArrayList of class X without position

If I have an ArrayList in a Java 6 program as follows:
ArrayList<Keyword> = new ArrayList<Keyword>();
Where a Keyword has an int keywordNo, String text, int frequency and a few other fields.
How can I retrieve an element from the ArrayList if I know the keywordText but not the position of the element or the value of field keywordNo?
I know that I could use a loop and simply read through the ArrayList comparing Strings until I find the element but is there any better alternative?
You would have to iterate over every element in the list with a loop. For each iteration you will need to get the current element and check its value.
For faster access you should use a Map<String, Keyword> where the String is the keywordText.
You can put your keywords in a Map like this:
Map<String, Keyword> keywordsMap = new HashMap<String, Keyword>();
for (Keyword k : keywordList) {
keywordsMap.put(k.text, k);
}
Then if you want to access a particular Keyword you can make a call like this:
Keyword result = keywordsMap.get("somekeyword");

How to populate a List with a while loop without declaring a new object every time?

So, I'm trying to s a list of documents that contain a term and then enter the corresponding document_id and the term frequency into an array (of size 2). I then add this entry array into a List, so that the final List contains an all the entries. However, because the entry is passed by reference into the List, I have no idea how to accomplish this, since it rewrites itself every time. And due to the size of the data, my program runs out of memory if I try to declare a new int[] entry within the while loop. Any ideas on how to get pass this? I'm a but rusty on my Java. Thanks.
List<int[]> occurenceIndex = new ArrayList<>();
int[] entry = new int[2];
while (matchedDocs.next())
{
entry[0] = (matchedDocs.doc()); // Adds document id
entry[1] = (matchedDocs.freq()); // Adds term weight
occurenceIndex.add(entry);
}
Try to create a new object of the int array inside the loop.
List<int[]> occurenceIndex = new ArrayList<>();
while (matchedDocs.next())
{
int[] entry = new int[2];
entry[0] = (matchedDocs.doc()); // Adds document id
entry[1] = (matchedDocs.freq()); // Adds term weight
occurenceIndex.add(entry);
}
You have to put int[] entry = new int[2]; into the while loop
does it need to be an int, what about byte or short? if this isn't possible then the program needs to be re-factored as there is no way to store the arrays like this using the same array instance. – Neil Locketz 1 min ago edit
Consider using HashMap to store records.
Map<Integer, Integer> occurenceIdx = new HashMap<Integer, Integer>();
while(matchedDocs.next())
occurenceIdx.put(matchedDocs.doc(), matchedDocs.freq());
That's all the code you need to create the map. To retrieve value based on doc ID
docFreq = occurenceIdx.get(docId);
Please note that this will work ONLY if you have unique doc IDs. If not, you will have to improvise on this solution. I would probably make my map a HashMap<Integer, List<Integer>> to support multiple instances of docID

Getting two unique values from two Java Collection objects

I would like to get unique values from two Collection objects. How would I do that?
Example: Let us take two ArrayLists:
List bag1 = new ArrayList();
List bag2 = new ArrayList();
bag1.add("1");
bag1.add("2");
bag1.add("3");
bag1.add("7");
bag1.add("8");
bag1.add("9");
bag2.add("4");
bag2.add("5");
bag2.add("6");
bag2.add("7");
bag2.add("8");
bag2.add("9");
I need to get a result like --> 1,2,3 from bag1 and 4,5,6 from bag2
Could you please help me out?
Two things:
Use org.apache.commons.collections.CollectionUtils.disjunction(Collection a, Collection b);
Bag isn't the best variable name for a list. :)
You should take a look at Sets instead. The Java Collection has a few classes which deal with this. The idea is you could just the the set difference between the two collections, and you'll get your answer.
Use a 'set' to store your data. That way your collection will have unique elements as and when you add elements to the set.
See the javadoc over here: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html
have you tried...
bag1.removeAll(bag2);
If you want to keep bag1 and bag2 intact you can use a Set variable and pass all values of bag1 into the Set and then check for contains() like
Set set = new HashSet();
set.addAll(bag2);
for(Object o: bag1){
if(!set.contains(o)){
// Do whatever you want with bag1 elements
}
}
set.clear();
set.addAll(bag1);
for(Object o: bag2){
if(!set.contains(o)){
// Do whatever you want with bag2 elements
}
}
Use the removeAll method define in the Set interface.
Set intersect = new TreeSet(bag1);
intersect.removeAll(bag2);
List unique1 = Arrays.asList(intersect);
intersect = new TreeSet(bag2);
intersect.removeAll(bag1);
List unique2 = Arrays.asList(intersect);

Categories

Resources