Java ListArrays calling them

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Here the goal:
Make a program that takes a .csv file with 10,000 names and randomly assign them to one to one of 6 groups then prints out 6 files 1666 +1 names

So far I have a main class and two sub classes
The first class fileProc takes the given .csv file and makes an ArrayList with 10,000 names

Here is my problem in my 2nd class arrayProc

I have a arraylist of available "buckets" that way if a bucket reaches a 6th of the total input it is removed from the list. The availableBuckets array list hold 6 strings with the name of each available array.


How do I call the array that's name is at availableBuckets.get(#)?


Here is my code if needed:
Code:
package SortApp;

import java.util.*;

public class arrayProc {
	static ArrayList<String> bucket1;
	ArrayList<String> bucket2;
	ArrayList<String> bucket3;
	ArrayList<String> bucket4;
	ArrayList<String> bucket5;
	ArrayList<String> bucket6;
	
	public static boolean computeArray(ArrayList<String> ArrayProc){
		Random randomGenerator = new Random();
		boolean complete = false;
		int population = ArrayProc.size();
		int maxBucketSize = population /6; 
		ArrayList<String> availableBuckets = new ArrayList <String>();		
		availableBuckets.add("bucket1");
		availableBuckets.add("bucket2");
		availableBuckets.add("bucket3");
		availableBuckets.add("bucket4");
		availableBuckets.add("bucket5");
		availableBuckets.add("bucket6");
		
		for (int c=1; c < population;c++){
			int rand = randomGenerator.nextInt(availableBuckets.size());
			sk.add(rand, ArrayProc.get(c));

			
		}
		return complete;
		
	}
	
}

Thanks!
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,703
4,661
75
It looks like you need an array of ArrayList<String>s. My Java is quite rusty, but I think you want:

ArrayList<String>[] buckets = new ArrayList<String>[6];
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
It looks like you need an array of ArrayList<String>s. My Java is quite rusty, but I think you want:

ArrayList<String>[] buckets = new ArrayList<String>[6];

I was just about write up an example. This is exactly what you want.

To add to bucket 3, you would simply say:
buckets[3].Add("hello world");
 

Net

Golden Member
Aug 30, 2003
1,592
3
81
here's something to head you in a cleaner direction.

Code:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;


public class NameAllocator {
	
	public static void allocateNames(List<String> names) {
		Random randomGenerator = new Random();

		Map<Integer, List<String>> groups = new HashMap<Integer, List<String>>();
		groups.put(0, new ArrayList<String>());
		groups.put(1, new ArrayList<String>());
		groups.put(2, new ArrayList<String>());
		groups.put(3, new ArrayList<String>());
		groups.put(4, new ArrayList<String>());
		groups.put(5, new ArrayList<String>());

		for (int i = 0; i < names.size(); i++) {
			groups.get(randomGenerator.nextInt(6)).add(names.get(i));
		}

		Iterator<Map.Entry<Integer, List<String>>> it = groups.entrySet().iterator();
		while(it.hasNext()) {
			Map.Entry<Integer, List<String>> result = (Map.Entry<Integer, List<String>>) it.next();
			System.out.println(result.getKey() + ": " + result.getValue());
		}
	}

}

have fun :)
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
I was just about write up an example. This is exactly what you want.

To add to bucket 3, you would simply say:
buckets[3].Add("hello world");

It looks like you need an array of ArrayList<String>s. My Java is quite rusty, but I think you want:

ArrayList<String>[] buckets = new ArrayList<String>[6];

I wanted to have availableBuckets there so I can remove a bucket once it fills up with availableBuckets[1].remove

I'm somewhat confused :\

Thanks guys for all the help!
 

Leros

Lifer
Jul 11, 2004
21,867
7
81
I wanted to have availableBuckets there so I can remove a bucket once it fills up with availableBuckets[1].remove

I'm somewhat confused :\

Thanks guys for all the help!

In that case, you can use an arraylist instead an arrray

ArrayList<ArrayList<String>> availableBuckets = new ArrayList<ArrayList<String>>();
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Okay so here is my rewritten code:
Code:
 package SortApp;

import java.util.*;

public class arrayProc {
	static ArrayList<String> bucket1 = null;
	static ArrayList<String> bucket2 = null;
	static ArrayList<String> bucket3 = null;
	static ArrayList<String> bucket4 = null;
	static ArrayList<String> bucket5 = null;
	static ArrayList<String> bucket6 = null;
	
	public static boolean computeArray(ArrayList<String> ArrayProc){

		Random randomGenerator = new Random();
		String curentBucket; 
		boolean complete = false;
		int population = ArrayProc.size();
		int maxBucketSize = population /6; 
		ArrayList<String> availableBuckets = new ArrayList <String>();	
		availableBuckets.add("bucket1");
		availableBuckets.add("bucket2");
		availableBuckets.add("bucket3");
		availableBuckets.add("bucket4");
		availableBuckets.add("bucket5");
		availableBuckets.add("bucket6");

		
		
		for (int c=0; c < population;c++){
			int rand = randomGenerator.nextInt(availableBuckets.size());
			if(availableBuckets.get(rand) == "bucket1"){
				bucket1.add(ArrayProc.get(c));
				if(bucket1.size() == maxBucketSize){
					availableBuckets.remove(rand);
				}
			}
			if(availableBuckets.get(rand) == "bucket2"){
				bucket2.add(ArrayProc.get(c));
				if(bucket2.size() == maxBucketSize){
					availableBuckets.remove(rand);
				}
			}
			if(availableBuckets.get(rand) == "bucket3"){
				bucket3.add(ArrayProc.get(c));
				if(bucket3.size() == maxBucketSize){
					availableBuckets.remove(rand);
				}
			}
			if(availableBuckets.get(rand) == "bucket4"){
				bucket4.add(ArrayProc.get(c));
				if(bucket4.size() == maxBucketSize){
					availableBuckets.remove(rand);
				}
			}
			if(availableBuckets.get(rand) == "bucket5"){
[B]				bucket5.add(ArrayProc.get(c));
[/B]				if(bucket5.size() == maxBucketSize){
					availableBuckets.remove(rand);
				}
			}
			if(availableBuckets.get(rand) == "bucket6"){
				bucket6.add(ArrayProc.get(c));
				if(bucket6.size() == maxBucketSize){
					availableBuckets.remove(rand);
				}
			}



		}
		return complete;
		
	}
	
}

I bolded the line I get a error in:

Exception in thread "main" java.lang.NullPointerException
at SortApp.arrayProc.computeArray(arrayProc.java:57)

:(

Any ideas?
 
Sep 29, 2004
18,656
68
91
what mundane said

Also, I think rand might be out of range on ocasion the way things are coded. Ya, if arrayProc is greater than 36, it apepars this could happen.

int rand = randomGenerator.nextInt(availableBuckets.size());
should be
int rand = randomGenerator.nextInt(maxBucketSize);
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Unless I'm reading it incorrectly, you never created the actual 'buckets', hence the null pointer exception.

I think I did:

Code:
static ArrayList<String> bucket1 = null;
	static ArrayList<String> bucket2 = null;
	static ArrayList<String> bucket3 = null;
	static ArrayList<String> bucket4 = null;
	static ArrayList<String> bucket5 = null;
	static ArrayList<String> bucket6 = null;
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
what mundane said

Also, I think rand might be out of range on ocasion the way things are coded. Ya, if arrayProc is greater than 36, it apepars this could happen.

int rand = randomGenerator.nextInt(availableBuckets.size());
should be
int rand = randomGenerator.nextInt(maxBucketSize);

Thanks guys for helping!

well I did it that way so it would pick a random bucket
If i did it the 2nd way wouldn't it just pick a number for a random bucket entry.
 

alfa147x

Lifer
Jul 14, 2005
29,307
106
106
Well I got it working

Rewrote most of that class!

Code:
package sort;

import java.util.*;

public class ArrayProc {
	private ArrayList<String>[] buckets;
	
	@SuppressWarnings("unchecked")
	public ArrayProc()
	{
		buckets = new ArrayList[6];
		for(int i = 0; i < buckets.length; i++)
			buckets[i] = new ArrayList<String>();
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean computeArray(ArrayList<String> list){

		Random rand = new Random();
		int max = list.size() / 6;
		
		ArrayList avaliableBuckets = new ArrayList();
		for(int i = 0; i < buckets.length; i++)
			avaliableBuckets.add(i);
		
		while(!list.isEmpty())
		{
			int bucket = rand.nextInt(avaliableBuckets.size());
			buckets[bucket].add(list.get(0));
			if(buckets[bucket].size() >= max)
				for(int i = 0; i < avaliableBuckets.size(); i++)
					if(avaliableBuckets.get(i).equals(bucket))
						avaliableBuckets.remove(i);
			list.remove(0);
		}
		return true;
	}
	
	public ArrayList<String>[] getBuckets()
	{
		return buckets;
	}
}

Thanks guys! You help is greatly appreciated!

Remember today is the Marine Corps birthday! Wish a Marine happy
birthday!