Consider a List which have 60 or more elements. I want to break it into 6 List and add those to List. I am doing that because i want to send data to jsp in tabular format. Anyway, i am doing that but wanted to know if the way in which i am doing is good or not, cause i believe something better exist. Below is my code.
List<String> rollsAll = // from db
List<List<String>> rolls = new ArrayList<List<String>>();
int i=0;
for(String roll:rollsAll){
if(i<10)
{
if(i==0)
{
rolls.add(new ArrayList());
}
rolls.get(0).add(roll);
i++;
continue;
}
else if(i<20)
{
if(i==10)
{
rolls.add(new ArrayList());
}
rolls.get(1).add(roll);
i++;
continue;
}
else if(i<30)
{
if(i==20)
{
rolls.add(new ArrayList());
}
rolls.get(2).add(roll);
i++;
continue;
}else if(i<40)
{
if(i==30)
{
rolls.add(new ArrayList());
}
rolls.get(3).add(roll);
i++;
continue;
}else if(i<50)
{
if(i==40)
{
rolls.add(new ArrayList());
}
rolls.get(4).add(roll);
i++;
continue;
}else if(i<60)
{
if(i==50)
{
rolls.add(new ArrayList());
}
rolls.get(5).add(roll);
i++;
continue;
}else if(i<70)
{
if(i==60)
{
rolls.add(new ArrayList());
}
rolls.get(6).add(roll);
i++;
continue;
}else if(i<80)
{
if(i==70)
{
rolls.add(new ArrayList());
}
rolls.get(7).add(roll);
i++;
continue;
}else if(i<90)
{
if(i==80)
{
rolls.add(new ArrayList());
}
rolls.get(8).add(roll);
i++;
continue;
}else if(i<100)
{
if(i==90)
{
rolls.add(new ArrayList());
}
rolls.get(9).add(roll);
i++;
continue;
}else if(i<110)
{
if(i==100)
{
rolls.add(new ArrayList());
}
rolls.get(10).add(roll);
i++;
continue;
}else if(i<120)
{
if(i==110)
{
rolls.add(new ArrayList());
}
rolls.get(11).add(roll);
i++;
continue;
}
}
Thanks and Regards
Just walk the list 10 items at a time and use List.subList to grab the chunk that you need.
The below code does this and defensively copies the sub list.
int nPerSublist = 10;
List<String> rollsAll = // from db
List<List<String>> rolls = new ArrayList<List<String>>(
(rollsAll.size() + nPerSublist - 1) / nPerSublist);
for (int i = 0, n = rollsAll.size(); i < n; i += nPerSublist) {
rolls.add(new ArrayList<String>(rollsAll.subList(i, Math.min(i + nPerSublist, n))));
}
Using List.subList will produce an elegant way to achieve what you want:
Suppose I got a List of 1000 numbers and I want to split them into groups of 70:
List<Integer> numbers = new ArrayList<Integer>();
for (int i = 1; i <= 1000; i++) {
numbers.add(Integer.valueOf(i));
}
int totalItems = numbers.size();
int itemPerGroup = 70;
int totalGroup = (totalItems / itemPerGroup) + 1;
List<List<Integer>> groups = new ArrayList<List<Integer>>();
for (int groupCount = 1; groupCount <= totalGroup; groupCount++) {
int groupStartIndex = (groupCount - 1) * itemPerGroup;
int groupEndIndex = Math.min(numbers.size(), groupCount * itemPerGroup);
groups.add(numbers.subList(groupStartIndex, groupEndIndex));
}
I found this implementation on the web that you can use to store your data into a 2dArrayList, add this as a class to your project and you can use the methods there:
import java.util.ArrayList;
public class ArrayList2d<Type>
{
ArrayList<ArrayList<Type>> array;
public ArrayList2d()
{
array = new ArrayList<ArrayList<Type>>();
}
/**
* ensures a minimum capacity of num rows. Note that this does not guarantee
* that there are that many rows.
*
* #param num
*/
public void ensureCapacity(int num)
{
array.ensureCapacity(num);
}
/**
* Ensures that the given row has at least the given capacity. Note that
* this method will also ensure that getNumRows() >= row
*
* #param row
* #param num
*/
public void ensureCapacity(int row, int num)
{
ensureCapacity(row);
while (row < getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).ensureCapacity(num);
}
/**
* Adds an item at the end of the specified row. This will guarantee that at least row rows exist.
*/
public void Add(Type data, int row)
{
ensureCapacity(row);
while(row >= getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).add(data);
}
public Type get(int row, int col)
{
return array.get(row).get(col);
}
public void set(int row, int col, Type data)
{
array.get(row).set(col,data);
}
public void remove(int row, int col)
{
array.get(row).remove(col);
}
public boolean contains(Type data)
{
for (int i = 0; i < array.size(); i++)
{
if (array.get(i).contains(data))
{
return true;
}
}
return false;
}
public int getNumRows()
{
return array.size();
}
public int getNumCols(int row)
{
return array.get(row).size();
}
}
You can use that:
private static final int SIZE = 10; // size of an inner list
public List<List<String>> partition(final List<String> rolls)
{
final List<List<String>> ret = new ArrayList<List<String>>();
List<String> list;
int i = 0;
for (final String roll: rolls) {
if (i % SIZE == 0) {
list = new ArrayList<String>();
ret.add(list);
}
list.add(roll);
i++;
}
return ret;
}
Something like
List<String> rollsAll = // from db
List<List<String>> rolls = new ArrayList<List<String>>();
int size = rollsAll.size();
for (int i = 0; i < size / 10; i++) {
rolls.add(new ArrayList<String>(rollsAll.subList(10*i, 10*(i+1)));
}
// handle last part if size not divisible by 10
if (size % 10 > 0) {
rolls.add(new ArrayList<String>(rollsAll.subList(10 * (size / 10), size)));
}
List<List<String>> rolls = new ArrayList<List<String>>();
int i=0;
int currentArrayIndex = 0;
List<String> currentArray = null;
for(String roll:rollsAll){
if( (currentArrayIndex = i %10 ) ==0)
rolls.add(currentArray = new ArrayList());
currentArray.add(roll); i++;
}
Your approach is ok and other solutions are really good, but you're actually hardcoding the intervals to create the sublists. Instead, a simple counter and a elementsPerList variable could reduce your code to something more versatile:
public List<List<String>> splitList(List<String> original, int elementsPerList) {
List<List<String>> result = new ArrayList<List<String>>();
List<String> current = new ArrayList<String>();
result.add(current);
for(int i = 0; i < original.size(); i++) {
if(i < result.size() * elementsPerList) {
current.add(original.get(0));
} else {
current = new ArrayList<String>();
result.add(current);
current.add(original.get(0));
}
}
return result;
}
You just need to invoke this method with your current list and 10 as the amount of desired elements per list. Should you ever need to vary the amount of elements to split, you just need to pass the new amount to this method.
Use subList(fromIndex, toIndex)
List oldList = new LinkedList<String>();
// Add your elements in oldList
List newList1 = oldList.subList(0, 5);
List newList2 = oldList.subList(6, 10);
Related
I know similar questions have been asked before but I have found the answers confusing. I am trying to make a program that will find every combination of an array-list with no repetitions and only of the maximum size. If the list has 4 items it should print out only the combinations with all 4 items present. This is what I have so far:
public main(){
UI.initialise();
UI.addButton("Test", this::testCreate);
UI.addButton("Quit", UI::quit);
}
public void createCombinations(ArrayList<String> list, String s, int depth) {
if (depth == 0) {
return;
}
depth --;
for (int i = 0; i < list.size(); i++) {
if (this.constrain(s + "_" + list.get(i), list.size())) {
UI.println(s + "_" + list.get(i));
}
createCombinations(list, s + "_" + list.get(i), depth);
}
}
public void testCreate() {
ArrayList<String> n = new ArrayList<String>();
n.add("A"); n.add("B"); n.add("C"); n.add("D");
this.createCombinations(n , "", n.size());
}
public boolean constrain(String s, int size) {
// Constrain to only the maximum length
if ((s.length() != size*2)) {
return false;
}
// Constrain to only combinations without repeats
Scanner scan = new Scanner(s).useDelimiter("_");
ArrayList<String> usedTokens = new ArrayList<String>();
String token;
while (scan.hasNext()) {
token = scan.next();
if (usedTokens.contains(token)) {
return false;
} else {
usedTokens.add(token);
}
}
// If we fully iterate over the loop then there are no repitions
return true;
}
public static void main(String[] args){
main obj = new main();
}
This prints out the following which is correct:
_A_B_C_D
_A_B_D_C
_A_C_B_D
_A_C_D_B
_A_D_B_C
_A_D_C_B
_B_A_C_D
_B_A_D_C
_B_C_A_D
_B_C_D_A
_B_D_A_C
_B_D_C_A
_C_A_B_D
_C_A_D_B
_C_B_A_D
_C_B_D_A
_C_D_A_B
_C_D_B_A
_D_A_B_C
_D_A_C_B
_D_B_A_C
_D_B_C_A
_D_C_A_B
_D_C_B_A
This works for small lists but is very inefficient for larger ones. I am aware that what I have done is completely wrong but I want to learn the correct way. Any help is really appreciated. Thanks in advance.
P.S. This is not homework, just for interest although I am a new CS student (if it wasn't obvious).
Implementing Heap's algorithm in Java:
import java.util.Arrays;
public class Main {
public static void swap(final Object[] array, final int index1, final int index2) {
final Object tmp = array[index1];
array[index1] = array[index2];
array[index2] = tmp;
}
public static void printPermutations_HeapsAlgorithm(final int n, final Object[] array) {
final int[] c = new int[n];
for (int i = 0; i < c.length; ++i)
c[i] = 0;
System.out.println(Arrays.toString(array)); //Consume first permutation.
int i=0;
while (i < n) {
if (c[i] < i) {
if ((i & 1) == 0)
swap(array, 0, i);
else
swap(array, c[i], i);
System.out.println(Arrays.toString(array)); //Consume permutation.
++c[i];
i=0;
}
else
c[i++] = 0;
}
}
public static void main(final String[] args) {
printPermutations_HeapsAlgorithm(4, new Character[]{'A', 'B', 'C', 'D'});
}
}
Possible duplicate of this.
I am trying to make a program that creates an ArrayList given the type as well as the values that will be put into the ArrayList. The input structure that we have to work with is "I 6 7 5 3 1 -1 2" with the I being the type Integer (or S for String, etc) and the first number (6) being how many values are in the ArrayList. I'm not sure how to instantiate the ArrayList.
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String type = scan.next();
int length = scan.nextInt();
int counter = 0;
if (type.equals("I")) {
ArrayList<Integer> A = new ArrayList<Integer>;
}
else if (type.equals("S")) {
ArrayList<String> A = new ArrayList<String>;
}
else if (type.equals("D")) {
ArrayList<Double> A = new ArrayList<Double>;
}
else {
System.out.println("Invalid type");
}
while (scan.hasNext() && counter<length) {
String s1 = scan.next();
A.add(s1);
counter += 1;
}
System.out.print(A);
}
//Removes any duplicate values in the arraylist by checking each value after it
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
ArrayList<E> inArray = list;
for (int i = 0; i<inArray.size(); i++) {
for (int j = i+1; j<inArray.size(); j++) {
if (inArray.get(i) == inArray.get(j)) {
inArray.remove(j);
}
}
}
return inArray;
}
//Shuffles the contents of the array
public static <E> void shuffle(ArrayList<E> list) {
E temp;
int index;
Random random = new Random();
for (int i = list.size()-1; i > 0; i--) {
index = random.nextInt(i + 1);
temp = list.get(index);
list.set(index, list.get(i));
list.set(i, temp);
}
System.out.print(list);
return;
}
//Returns the largest element in the given arraylist
public static <E extends Comparable<E>> E max(ArrayList<E> list) {
E max = Collections.max(list);
System.out.println(max);
return max;
}
I cannot in good conscious give you the answer you want, but rather I'll give you the answer you need.
DON'T DO THAT!
It serves no purpose at all. Datatype erasure at compile time of generics makes the ArrayList<Whatever> act equivalently to ArrayList<?> You cannot ascertain the generic type during runtime unless you type check the elements within the ArrayList
You might as well write this code, it'll give you the same exact results:
public static ArrayList<?> returnProper(String type) {
if(type.length() == 1 && "ISD".contains(type)) {
return new ArrayList();
} else {
System.out.println("Invalid type");
return null;
}
}
THUS, PLEASE DON'T DO THAT
Replace the second E with an "?" and then fix the method to return.
public static <T> ArrayList<?> returnProper(String type) {
if (type.equals("I")) {
return new ArrayList<Integer>();
} else if (type.equals("S")) {
return new ArrayList<String>();
} else if (type.equals("D")) {
return new ArrayList<Double>();
} else {
System.out.println("Invalid type");
}
return null;
}
Could someone help me understand where this time limited exceeding comes from? The context is that in this threeSum method, given an array, I'm trying to record all possible combinations of three numbers that add up to 0. The original question comes from : https://leetcode.com/problems/3sum/
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> retList = new ArrayList<List<Integer>>();
Arrays.sort(nums); // O(nlogn)
for (int i=0; i<nums.length-1; i++){
int pleft;
int pright;
if (i!=0){
pleft = i-1;
while((nums[pleft]==nums[i]) && (pleft-1 >=0)){
pleft--;
}
} else {
pleft = i;
}
if (i!=nums.length-2){
pright = i+1;
while((nums[pright]==nums[i]) && (pright+1 < nums.length-1)){
pright++;
}
} else {
pright = i;
}
int sum;
while (true){
sum = nums[pleft]+nums[pright]+nums[i];
if (sum==0){
List<Integer> temp = new ArrayList<Integer>();
temp.add(nums[pleft]);
temp.add(nums[pright]);
temp.add(nums[i]);
retList.add(temp);
if (pleft-1>=0) pleft--;
if (pright+1<nums.length-1) pright++;
} else if (sum>0){
if (pleft-1>=0) pleft--;
} else { // less than zero
if (pright+1<nums.length-1) pright++;
}
}
}
return retList;
}
}
You're not breaking out of your while(true) loop. That code will just run forever and you won't return a value. You need to add a break or change the while (true) to while (condition)
I have a programming assignment for an introductory level Java class (the subset sum problem) - for some reason, my recursive method isn't executing properly (it just goes straight to the end of the method and prints out the sorted list). Any help would be appreciated - I'm a newbie and recursive functions are really confusing to me.
package programmingassignment3;
import java.io.*;
import java.util.*;
public class ProgrammingAssignment3 {
static int TARGET = 10;
static ArrayList<Integer> list = new ArrayList<>();
static int SIZE = list.size();
public static void main(String[] args) {
populateSortSet();
sumInt(list);
recursiveSS(list);
}//main
public static void populateSortSet() {
try {
File f = new File("set0.txt");
Scanner input = new Scanner(f);
while (input.hasNext()) {
int ele = input.nextInt();
if (ele < TARGET && !list.contains(ele)) {
list.add(ele);
}//if
}//while
Collections.sort(list);
}//try
catch (IOException e) {
e.printStackTrace();
}//catch
}//populateSet
public static void recursiveSS(ArrayList<Integer> Alist) {
if (Alist.size() == SIZE) {
if (sumInt(Alist) == TARGET) {
System.out.println("The integers that equal " + TARGET + "are: " + Alist);
} //if==TARGET
}//if==SIZE
else {
for (int i = 0; i < SIZE; i++) {
ArrayList<Integer> list1 = new ArrayList<>(Alist);
ArrayList<Integer> list0 = new ArrayList<>(Alist);
list1.add(1);
list0.add(0);
if (sumInt(list0) < TARGET) {
recursiveSS(list0);
}//if
if (sumInt(list1) < TARGET) {
recursiveSS(list1);
}//if
}//for
}//else
System.out.println("echo" + Alist);
}//recursiveSS
public static int sumInt(ArrayList<Integer> Alist) {
int sum = 0;
for (int i = 0; i < SIZE - 1; i++) {
sum += Alist.get(i);
}//for
if (Alist.size() == TARGET) {
sum += Alist.get(Alist.size() - 1);
}//if
return sum;
}//sumInt
}//class
This thing that you do at class level:
static ArrayList<Integer> list = new ArrayList<>();
static int SIZE = list.size();
means that SIZE will be initiated to 0, and stay 0 (even if you add elements to the list.)
This means that the code inside the for-loop will be executed 0 times.
Try something like:
public class ProgrammingAssignment3 {
private static int initialSize;
//...
public static void populateSortSet() {
//populate the list
initialSize = list.size();
}
So you don't set the value of the size variable until the list is actually populated.
That being said, there a quite a few other strange things in your code, so I think you need to specify exactly what you are trying to solve here.
Here's how I'd do it. I hope it clarifies the stopping condition and the recursion. As you can see, static methods are not an issue:
import java.util.ArrayList;
import java.util.List;
/**
* Demo of recursion
* User: mduffy
* Date: 10/3/2014
* Time: 10:56 AM
* #link http://stackoverflow.com/questions/26179574/recursive-method-not-properly-executing?noredirect=1#comment41047653_26179574
*/
public class RecursionDemo {
public static void main(String[] args) {
List<Integer> values = new ArrayList<Integer>();
for (String arg : args) {
values.add(Integer.valueOf(arg));
}
System.out.println(String.format("input values : %s", values));
System.out.println(String.format("iterative sum: %d", getSumUsingIteration(values)));
System.out.println(String.format("recursive sum: %d", getSumUsingRecursion(values)));
}
public static int getSumUsingIteration(List<Integer> values) {
int sum = 0;
if (values != null) {
for (int value : values) {
sum += value;
}
}
return sum;
}
public static int getSumUsingRecursion(List<Integer> values) {
if ((values == null) || (values.size() == 0)) {
return 0;
} else {
if (values.size() == 1) { // This is the stopping condition
return values.get(0);
} else {
return values.get(0) + getSumUsingRecursion(values.subList(1, values.size())); // Here is recursion
}
}
}
}
Here is the case I used to test it:
input values : [1, 2, 3, 4, 5, 6]
iterative sum: 21
recursive sum: 21
Process finished with exit code 0
Thanks everyone. I really appreciate the help. I did figure out the problem and the solution is as follows (closing brace comments removed for the reading pleasure of #duffymo ):
public class ProgrammingAssignment3 {
static int TARGET = 6233;
static ArrayList<Integer> set = new ArrayList<>();
static int SIZE;
static int count = 0;
public static void populateSortSet() {
try {
File f = new File("set3.txt");
Scanner input = new Scanner(f);
while (input.hasNext()) {
int ele = input.nextInt();
if (ele < TARGET && !set.contains(ele)) {
set.add(ele);
}
}
Collections.sort(set);
SIZE = set.size();
System.out.println("The original sorted set: " + set + "\t subset sum = " + TARGET);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void recursiveSS(ArrayList<Integer> list) {
if (list.size() == SIZE) {
if (sumInt(list) == TARGET) {
System.out.print("The Bit subset is: " + list + "\t");
System.out.println("The subset is: " + getSubset(list));
count++;
}
}
else {
ArrayList<Integer> list1 = new ArrayList<>(list);//instantiate list1
ArrayList<Integer> list0 = new ArrayList<>(list);//instantiate list0
list1.add(1);
list0.add(0);
if (sumInt(list0) <= TARGET) {
recursiveSS(list0);
}
if (sumInt(list1) <= TARGET) {
recursiveSS(list1);
}
}
}
public static int sumInt(ArrayList<Integer> list) {
int sum = 0;
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 1) {
sum += set.get(i);
}
}
return sum;
}
public static ArrayList<Integer> getSubset(ArrayList<Integer> list) {
ArrayList<Integer> l = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == 1) {
l.add(set.get(i));
}
}
return l;
}
}
I have this class and in the printVotes method I had to do the if statement every time to print each votes. Is there any way to combine both the if statements. Could I print all the names of the candidates and the number of votes they got at the same time?
public class TestCandidate {
public static void main(String[] args)
{
Canidate[] canidate = new Canidate[5];
// create canidate
canidate[0] = new Canidate("John Smith", 5000);
canidate[1] = new Canidate("Mary Miller", 4000);
canidate[2] = new Canidate("Michael Duffy", 6000);
canidate[3] = new Canidate("Tim Robinson", 2500);
canidate[4] = new Canidate("Joe Ashtony", 1800);
printVotes(canidate) ;
}
public static void printVotes(Canidate [] List)
{
double max;
int index;
if (List.length != 0)
{
index = 0;
for (int i = 1; i < List.length; i++)
{
}
System.out.println(List[index]);
}
if (List.length != 0)
{
index = 1;
for (int i = 1; i < List.length; i++)
{
}
System.out.println(List[index]);
return;
}
}
}
If you pass in a List<Candidate> candidates; and assuming that each candidate has a List<Integer> Votes:
List<Integer> votes= new ArrayList<Integer>() ;
for(Candidate c:candidates)
{
votes.add(c.GetVote()) ;
}
for(Integer v:votes)
{
System.out.println(v);
}
You could override the Candidate class's toString() method like so:
public String toString() {
return "Candidate Name: " + this.name + "\nVotes: " + this.votes;
}
Then your printVotes method would look something like this:
public static void printVotes(Candidate[] list) {
for(Candidate c : list) {
System.out.println(c);
}
}
As someone else mentioned, avoid using capital letters in variable names especially in cases where words such as List are used. List is a collection type and can be easily confused.