How to break an ArrayList into groups? - java

I am trying to implement this method:
public ArrayList<ArrayList> groupWords(ArrayList<String> scrambledWords, int groupNumber);
The method takes an ArrayList of Strings and a number that represents the number of words in each group as parameters and then returns an ArrayList made of ArrayLists that contain groups of words according to the groupNumber parameter. For example, there is an ArrayList of 20 strings and I want to group that ArrayList into groups of 5 so I call the method like this:
ArrayList<ArrayList> groupedWords = groupWords(ArrayList, 5);
I am pretty sure that I need to have a for loop with another for loop nested inside, but I am not sure how to implement it.
How do I implement this method?

With Guava:
List<List<String>> groupedWords = Lists.partition(words, 5);

Something like this should work:
ArrayList<ArrayList<String>> grouped = new ArrayList<>();
for(int i = 0; i < words.size(); i++) {
int index = i/groupSize;
if(grouped.size()-1 < index)
grouped.add(new ArrayList<>());
grouped.get(index).add(words.get(i));
}
I haven't tested this code but basically I'm using the fact that integer division is always rounding to the next lowest Integer.
Example: 4/5=0.8 and is rounded to 0.

public ArrayList<ArrayList> groupWords(ArrayList<String> scrambledWords, int groupNumber){
int arraySize = scrambledWords.size();
int count = 0;
ArrayList<ArrayList> result = new ArrayList<>();
ArrayList<String> subResult = new ArrayList<>();
for(int i = 0 ; i < arraySize; i++){
if(count == groupNumber){
count = 0;
result.add(subResult);
subResult = new ArrayList<>();
}
subResult.add(scrambledWords.get(i));
count++;
}
return result;
}
This is simple Java Collections Soultion.
Suggestion : As a return type you should use ArrayList<ArrayList<String>>, and this should be the type for result also.

Related

How to check if a number is between a range

I have a problem for an online course I am doing. The question is:
Given an Integer x, write a program which generates random numbers between x and 0 until each number in this range has been generated at least once. Once all numbers in this range have been generated, the program should display the numbers which were generated.
I have written a program which I thought would solve this but am having problems with the checking if a number is in the range. Here is my code so far:
public static void main(String[] args) {
Random generator = new Random();
ArrayList<Integer> range = new ArrayList<Integer>();
ArrayList<Integer> generated = new ArrayList<Integer>();
int x = 10;
int count = 0;
for(int i = 0; i<x+1; i++){
range.add(i);
}
while(range.isEmpty() != true){
int temp = generator.nextInt(x-1);
count++;
generated.add(temp);
if(range.contains(temp)){
range.remove(temp);
}
}
}
}
My idea was to first create two arraylists. The first would hold all numbers between 0 and the given x. The second would contain the random numbers generated. I then fill the range arraylist with the range between 0 and x. My While loop then checks this range list to see if it is empty. If not, it generates a random number, adds it to my second arraylist. I then check if this number is in the range arraylist - if it is it removes it and carries on. The problem I am having is it is running into IndexOutOfBoundsException after a few goes. I think this is because I am removing the generated numbers from the arraylist. Can anyone help me with fixing this
EDIT: I cant use any collections or other APIs. This part of the course is mainly about using Arrays and loops etc, not advanced Java stuff.
remove is an overloaded method, there is remove(int) which removes the item at the index specified and there is remove(T) which removes the first object int the list that is equal to the argument you passed in
since you passed an int to the method not an Integer, the first method is chosen
the simpliest modification to your code is replacing range.remove(temp); with range.remove(range.indexOf(temp)); or range.remove((Integer)temp)
also you have to call generator.nextInt(x+1); or else the program will be stuck in an infinite loop
You can just replace range.remove(temp); with range.removeIf(t -> t == temp);
Random generator = new Random();
ArrayList<Integer> range = new ArrayList<Integer>();
ArrayList<Integer> generated = new ArrayList<Integer>();
int x = 10;
int count = 0;
for(int i = 0; i<x+1; i++){
range.add(i);
}
while(range.isEmpty() != true){
int temp = generator.nextInt(x-1);
count++;
generated.add(temp);
if(range.contains(temp)){
range.removeIf(t -> t == temp);
}
}
OR You can use Iterator to remove from the List
for (Iterator<Integer> it = range.iterator(); it.hasNext(); ) {
Integer obj= it.next();
if (obj == temp) {
// Remove the current element from the iterator and the list.
it.remove();
break;
}
}
One more issue in your logic
int temp = generator.nextInt(x-1); The random number you are generating doesn't contain all the numbers. It should be int temp = generator.nextInt(x+2);
Hope the below will meet your requirement.
Random random = new Random();
int x = 3;
List<Integer> range = new ArrayList<>();
for(int i = 0; i <x+1; i++) {
range.add(i);
}
List<Integer> list = new ArrayList<>();
while (!list.containsAll(range)) {
list.add(random.nextInt(x + 1));
}
System.out.println(list);

Enumerate the unordered pairs (2-combinations) of a set

I'd like to iterate through an ArrayList representing a set of Persons and compare the content of each Person with each other Person. The content are full of Hasmaps in this form. I need to compare the Value of the matching Key (Key is unique) and get the difference of the Integer. This should iterate through all the Hashmaps and for all the Persons in the Arraylist. But I shouldn't compare p.e. Person A with Person C and then Person C again with Person A.
How can I code it?
I'm struggling for the last 3 hours.
public Integer comparison(){
ArrayList<HashMap> personList = new ArrayList<>();
for(int i = 0; i < personList.size(); i++){
HashMap<String, Integer> persons = new HashMap<>();
for(int j = i+1; j<persons.size(); j++){
// sum up the differences
}
difference+=difference;
}
return difference;
}
This topic in mathematics uses what are called Combinations wherein you need to find the set of all k-combinations of a set (persons A, B, and C). In this case it is simple to get all the combinations, because you know it is always only required to choose two elements; that is, k=2. See my outer loop and inner loop below for an easy way of achieving this:
for(int a=0; a < personList.size()-1 /* stop before last */; a++) {
for(int b=a+1 /* start after first */; b < personList.size(); b++) {
int sumDiff = 0;
System.out.print("person"+(char)('A'+a)+" compared with person"+(char)('A'+b)+" = ");
Set<String> keys = personList.get(a).keySet();
keys.retainAll(personList.get(b).keySet()); // keys in both only
for(String key : keys) {
sumDiff += Math.abs(personList.get(a).get(key)-personList.get(b).get(key));
}
System.out.println(sumDiff);
}
}
Output:
personA compared with personB = 11
personA compared with personC = 8
personB compared with personC = 9
First of all, it is very unclear as to what you want to do. I am assuming that you have been given the personList and you pass it to the function you are writing. If you want the result as a list of individual comments you need to add them to a list and return a List instead of an Integer.
The following code for your example should return a List which contains the values {11,8,9}. If you want the sum of these values like 11+8+9 then instead of adding each difference to a list add it to a variable initialized to 0 and declared outside the 1st for loop.
public List<Integer> comparison(ArrayList<HashMap> personList){
List<Integer> result = new ArrayList<Integer>();
//int res = 0;
for(int i = 0; i < personList.size(); i++){
for(int j=i+1; j< personList.size(); j++){
int difference = 0
for(Map.Entry<String, Object> entry : personList.get(i).entrySet()){
String key = entry.getKey();
int val = entry.getValue();
difference += Math.abs(personList.get(j).get(key) - val);
}
}
//res += difference
result.add(difference);
}
//return res;
return result;
}

How to set size of two dimensional ArrayList?

So I'm trying to make an two dimensional ArrayList which has a set amount of ArrayLists, then each of those ArrayLists can contain as much as needed. I'm aware that arrays dynamically change size, but I'm trying to guarantee that is has at least a certain size in this case.
ArrayList<ArrayList<Integer>> integers = new ArrayList<ArrayList<Integer>>(10);
This doesn't work. I want to be able to set the location of a new Integer to one of the first dimension's indices, like so:
integers.get(7).add(new Integer(42));
This just gives me an IndexOutOfBoundsException, as though there are no Integer ArrayLists within the ArrayList. Is there a way to do this? I'm sure it's something simple I'm not seeing.
Array lists do not work like this. They are not arrays.
The list you created is backed by array of at least 10 elements, but itself it does not contain any, so you cannot refer to 7th or actually any one element.
integers.size() would return 0
integers.isEmpty() would return true
integers.get(0) would throw
Moreover, the list you initialized needs to be filled with lists themselves:
for (int i = 0; i < 10; ++i) {
row = new ArrayList<Integer>()
integers.add(row);
}
// now integers is a 10-element list of empty lists
Alternatively you could use primitive arrays (if you want to have a fixed-size rectangle).
int integers[][] = new int[10][];
for (int i = 0; i < integers.length; ++i) {
integers[i] = new int[10]; // rows are initialized to 0, as int is primitive
}
for (final int[] arr : integers) {
System.out.println(Arrays.toString(arr));
}
You can use a nested loop for this. Here is a short example:
import java.util.ArrayList;
public class PopulateArray {
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> integers = new ArrayList<ArrayList<Integer>>();
int num_arrays_ to_populate = 10;
int num_indices_to_populate = 10;
for(int i = 0; i < num_arrays_to_populate; i++) {
integers.add(new ArrayList<Integer>());
for(int j = 0; j < num_indices_to_populate; j++) {
integers.get(i).add(0);
}
}
}
}
This would create an ArrayList of ArrayLists of ints and fill the top ArrayList with 10 ArrayLists and put a 0 in the first 10 cells of each. Obviously you can change any of those numbers to do what you want.
Note/Disclaimer: I wrote this on my phone, so if I missed a brace or semicolon, just comment and I’ll add it. The logic is there, though.

Creating x ArrayLists

I use a variable class which stores an integer. If the integer is 13, I need 13 arraylists to be created. Any idea as to how I would do this?
This is what is in my while loop:
ArrayList<Integer[]> list + count = new ArrayList<>();
With the following:
ArrayList<ArrayList<Integer>> myLists = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < 4; i++) {
ArrayList<Integer> newList = new ArrayList<Integer>();
myLists.add(newList);
}
Let's say I need the integer 9 to go into the 3rd array list and the integer 3 to go to the 2nd array list. How would i do that?
What are you trying to achieve?
You can create an ArrayList of ArrayLists. So something like:
ArrayList<ArrayList<Integer>> myLists = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < numOfLists; i++) {
ArrayList<Integer> newList = new ArrayList<Integer>();
myLists.add(newList);
}
That way you'll have myLists with all the lists you created.
By concatinating the variable to make another variable name is not possible. The compiler should already told you about this. You can accomplish this by using another ArrayList
List<List<Integer[]>> mainList = new ArrayList<List<Integer[]>>();
for(int i = 0; i < 13; i++){
mainList.add(new ArrayList<Integer[]>());
}
If you want to identify each of the ArrayList by a name you could use a Map.
Map<String, ArrayList<Integer[]>> listsMap = new HashMap<>();
for(int i = 0; i < 13; i++){
listsMap.add("list" + i, new ArrayList<>());
}
Then, if you need to add an integer to a particular list you could do this:
listsMap.get("list3").add(new Integer[]{3});

Java - How to repopulate an arraylist with previously known elements

I have an arraylist populated by four elements, the order of which is random (they are put here by random from another arraylist). I then have a for loop that repeats 10 times, at the end of each repetition I use the clear methods to clear all the elements of the arraylist. However, when I start a new repetition, I would like to repopulate my arraylist with the old (previously worked with) elements that were members of the list in the previous repetition, so that I can use the elements again. And I would like to repeat that until I get out of my 10-repetition for loop. Is there any way to achieve this at all?
Code in addition to my question:
ArrayList<String> answerPegs = new ArrayList<String>();
// add element to ArrayList
ArrayList<String> mySecretAnswer = new ArrayList<String>();
for (int n = 4; n > 0; n--)
{
//populate mySecretAnswer with elements from answerPegs
}
ArrayList<String> clone1 = mySecretAnswer;
for (int q = 0; q < 10; q++) {
for (o = 0; o < 4; o++)
{
}
// called clear() method here
} // END OF 10-ROW LOOP
I would suggest simply having 2 lists - keep a pristine copy of the original list, and then iterate over + clear a copy of that list.
public void doRepetitions(List<Object> original)
{
for( int i=0; i<10; i++ )
{
List<Object> working = new ArrayList<Object>( original );
doStuffWithList(working);
}
}
Edit:
Since you've posted your code, I can give a more specific answer:
You can change your clone to be:
ArrayList<String> clone1 = new ArrayList<String>(mySecretAnswer);
And then move that to be inside your for loop:
for (int q = 0; q < 10; q++)
{
ArrayList<String> clone1 = new ArrayList<String>(mySecretAnswer);
// ....
}
Could you use 2 loops nested and just have the inner loop be the for loop 10 times then clear once at the end

Categories