This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I need to create an array list using generics. My add method seems to work sometimes, however my get method appears to have a good amount of problems and i don't receive a compile error. However when i try to get an object from the Array list using my get method it throws a java out of bounds exception. here i what i have so far, and i am using BlueJ. Also, the instructions were to set the initial "illusion" length to zero.
public class AL <X> {
private X[]data;
private int count;
public AL() {
count = 0;
data = (X[]) new Object[0];
}
public void add (X v) {
if (data.length != count) {
data[count] = v;
count++;
} else {
X [] newdata = (X[]) new Object[data.length * 2];
for (int i = 0; i < data.length; i++) {
newdata[i] = data [i];
}
count++;
data = newdata;
}
}
public X get(int index) {
if (index >= count || index < 0) {
throw new ICantEven();
} else {
return data[index];
}
}
}
Your add method doesn't work, since the initial backing array you are using has a 0 length, which remains 0 even when you try to double it (since 0*2==0).
You also forgot to actually add the new element when you resize the backing array. If you hadn't forgot that, you'd get the exception in add.
First of all, change the initial size of the array created by your constructor to be positive :
data = (X[]) new Object[10];
Then add
data[count] = v;
to the else clause of your add method (just before count++;).
Your add method can be further simplified :
public AL()
{
count = 0;
data = (X[]) new Object[10];
}
public void add (X v)
{
// resize backing array if necessary
if (data.length == count)
{
X [] newdata = (X[]) new Object[data.length * 2];
for (int i = 0; i < data.length;i++ )
{
newdata[i] = data [i];
}
data = newdata;
}
// add new element
data[count] = v;
count++;
}
Related
I have a .csv that has data that looks like so:
First name Last initial,1/0,1/0,....
It is either a 1 or 0 depending on the persons choice from a txt file that contains 50 pairs of choices.
E.g. Apple
Pear
Dog
Cat
My issue is that my method (classSummary) that keeps count of how many people select the choices only goes halfway and the count is incorrect.
Here is my code as it stands, and any advice would be much appreciated:
public static void classSummary() {
for (Student student : data) {
int[] answers = student.getAnswers();
for (int i = 0; i < answers.length; i++) {
if (answers[i] == 0) {
choices.get(i).setCount(choices.get(i).getCount() + 1);
}
There are two issues in your approach:
You are checking only the case if (answers[i] == 0) you need an else part to handle the case where that is not true, i.e where answers[i] == 1
your answers array is only half the size of your choices list and the ith index in the array holds information about the i * 2 and i * 2 + 1 choices. But at the moment you associate the ith index of answers with the ith index of your choices list.
Change the code in the inner loop of your classSummary method from:
public static void classSummary() {
for (Student student : data) {
int[] answers = student.getAnswers();
for (int i = 0; i < answers.length; i++) {
if (answers[i] == 0) {
choices.get(i).setCount(choices.get(i).getCount() + 1);
}
}
}
}
to
public static void classSummary() {
for (Student student : data) {
int[] answers = student.getAnswers();
for (int i = 0; i < answers.length; i++) {
if (answers[i] == 0) {
choices.get(i * 2).setCount(choices.get(i * 2).getCount() + 1);
} else {
choices.get(i * 2 + 1).setCount(choices.get(i * 2 + 1).getCount() + 1);
}
}
}
}
You could even shorten the code that #Eritrean suggested to:
public static void classSummary() {
for (Student student : data) {
int[] answers = student.getAnswers();
for (int i = 0; i < answers.length; i++) {
int index = i * 2 + answers[i];
choices.get(index).setCount(choices.get(index) + 1);
}
}
}
But you really should validate the inputs of that text file with the answers like:
while (inFile2.hasNextLine()) {
String line = inFile2.nextLine();
String[] x = line.split(",");
String name = x[0];
int[] answers = new int[x.length - 1];
for (int i = 1; i < x.length; i++) {
answers[i - 1] = x[i].trim().equals("0") ? 0 : 1;
}
Student student = new Student(name, answers);
data.add(student);
}
And you could either use a Map<Integer> instead of the ArrayList<Choices> (this means you would not need to implement the whole Choices class). In that case you either use a HashMap if the order of choices does not matter in the result, or a LinkedHashMap if the order of choices shall be kept in the order they appear in the input file.
Or you implement a method increment in your Choices class to improve the readability of your code:
static class Choices {
// ... existing parts go here
void increment() {
count++;
}
}
Instead of choices.get(index).setCount(choices.get(index) + 1); you would then only write choices.get(index).increment();
I am currently filling in a section of code and in it one of the methods I have to make a new array double the size and fill it in with the old values, which I did, but how do I make it so that the new array is the one that is used from now on if the method doesn't return anything?
private void upSize()
{
String[] biggerArr = new String[theArray.length*2];
for( int i = 0; i < theArray.length; i++)
biggerArr[i] = theArray[i];
}
So obviously this code is built to not return anything, but when it gets called somewhere like here, how do I actually increase the size of that array so that it can fit the new "Added" element.
public boolean add( T element ){
if (size() == theArray.length) upSize();
theArray[count++] = element;
return true;
}
I'm sorry if this was something that already has an answered thread, I did a lot of searching through and couldn't find anything that touched on this specifically.
You have to return new array and assign it to the existing reference, like this:
private String[] upSize()
{
String[] biggerArr = new String[theArray.length*2];
for( int i = 0; i < theArray.length; i++)
biggerArr[i] = theArray[i];
return biggerArr;
}
public boolean add( T element ){
if (size() == theArray.length) theArray = upSize();
theArray[count++] = element;
return true;
}
But look at java.util.ArrayList, you won't have to reinvent the wheel :)
Learning about Arrays. I am not able to figure out why a new number is not added to the back of my existing array. I read in two textfiles in file_1.txt are the numbers '1 2 3' and in file_2.txt is the number '91'. Basically without the method of Void addBack() the program does what I expect, however by adding the method it seems not make a new Array. Even when I go over the elements[i] = elements[i-1] it won't print it as a whole. I am expecting to print for the first part
The numbers are: 1 2 3 and the second part The numbers are: 1 2 3 91.
public class ExampleLecture {
IntRow readIntRow(Scanner input) {
IntRow result = new IntRow();
while (input.hasNext()) {
result.add(input.nextInt());
}
return result;
}
IntRow setBack(Scanner input) {
IntRow result = new IntRow();
while(input.hasNext()) {
result.addBack(input.nextInt());
System.out.println("here");
}
return result;
}
void print(IntRow row) {
for (int i = 0; i < row.numberOfElements; i++) {
System.out.printf("%d ", row.elements[i]);
}
System.out.printf("\n");
}
void start() {
Scanner in = UIAuxiliaryMethods.askUserForInput().getScanner();
Scanner in2 =UIAuxiliaryMethods.askUserForInput().getScanner();
IntRow row = readIntRow(in);
IntRow row2 = setBack(in2);
System.out.printf("the numbers are: ");
print (row);
System.out.printf("the new numbers are: ");
print (row2);
}
public static void main(String[] args) {
new ExampleLecture().start();
}
}
package examplelecture;
class IntRow {
static final int MAX_NUMBER_OF_ELEMENTS = 250;
int[] elements;
int numberOfElements;
IntRow() {
elements = new int[MAX_NUMBER_OF_ELEMENTS];
numberOfElements = 0;
}
void add(int number) {
elements[numberOfElements] = number;
numberOfElements += 1;
}
void addBack(int number) {
for (int i = numberOfElements; i>0; i--) {
elements[i] = elements[i-1];
elements[i] = number;
}
}
}
You have 2 successive assignments which write to the same position:
elements[i] = elements[i-1];
elements[i] = number;
The value is alway overwritten with number, so the first statement has no effect.
Also in your addBack method your for cycle:
for (int i = numberOfElements; i>0; i--) {
What happens if numberOfElements is 0?
You call it addBack but it looks like a better name for the method is addFirst. Usually index 0 is considered the front, not the back.
First off, both the readIntRow() and setBack() methods create new IntRow objects row and row2. If you want the result to be appended to the first IntRow object created i.e. to row , you should call:
IntRow row = readIntRow(in);
IntRow row2 = row.setBack(in2);
and setBack() needs to be modified to:
IntRow setBack(Scanner input) {
while(input.hasNext()) {
this.add(input.nextInt());
System.out.println("here");
}
return this;
}
Note that in setBack(), if you are trying to append numbers to the end of the IntRow object, you should call add() instead of addBack() as above. If you are trying to add to the front, you should call addBack() [and it might be better to call it addFront() instead].
Also, in the implementation of addBack(), if you are trying to add to the front of the IntRow object, the element[i] = number operation should take place only once, after the loop. Otherwise all the values in indices <= numberOfElements would be overwritten with number.
void addBack(int number) {
for (int i = numberOfElements; i>0; i--) {
elements[i] = elements[i-1];
}
elements[0] = number;
}
Admittedly it is not entirely clear what you are trying to accomplish. But you may have several problems. The first is as follows:
IntRow setBack(Scanner input) {
IntRow result = new IntRow();
while (input.hasNext()) {
result.addBack(input.nextInt());
System.out.println("here");
}
return result;
}
IntRow has nothing in it since it is new. So all you are doing is iterating over the new file which has just 91 in it. Remember, result has no items. So it won't even iterate once in addBack.
So just do the following:
Change your addBack method to just add the numbers. Why use a loop to cascade down the elements since you are doing this within the same instance of IntRow? Just add it on to the end using the numberofElements as the next index.
void addBack(int number) {
elements[numberOfElements++] = number;
}
If you want to copy the contents of one IntRow object to another you would need another method in the IntRow class. Something like:
public void copy(IntRow r) {
for (int i = 0; i < r.numerOfElements; i++) {
elements[i] = r.elements[i];
}
numerOfElements = r.numberOfElements;
}
And keeping with good design it might be better to return numberOfElements in a method such as public int size();
I was able to make the Constructor and capacity methods to works but don;t know why size(),isFull() and isEmpty() fails.I believe its pretty simple but i am just unable to see a minor error and fix it.Hope someone can clarify what i am doing wrong with thorough explaination.Also,my constructor works with the test file and it passes,but just want to know Is my constructor correct as specified by question?
import java.util.Arrays;
import java.util.Iterator;
public class SortedArray<T extends Comparable> implements
java.lang.Iterable<T> {
public SortedArray(int capacity) {
this.array = (T[]) new Comparable[0];
this.capacity = capacity;
this.size = 0;
}
public SortedArray(int capacity, T[] data) {
if(capacity > data.length)
{
this.capacity = capacity;
}
else {
this.capacity = data.length;
}
this.size = data.length;
this.array = (T[]) new Comparable[0];
}
final public int size() {
return this.size
}
final public int capacity() {
return this.capacity;
}
final boolean isEmpty() {
return size == 0;
}
final boolean isFull(){
return size == capacity;
}
#Override
final public Iterator<T> iterator() {
// Do not modify this method.
return Arrays.stream(array).iterator();
}
// Do not modify these data members.
final private T[] array; // Storage for the array's element
private int size; // Current size of the array
final private int capacity; // Maximum size of the array
}
//// Test File:
#Test
public void testConstructor() {
System.out.println("Constructors");
SortedArray array = new SortedArray(20);
assertEquals(array.size(), 0);
assertEquals(array.capacity(), 20);
Integer[] data = {1, 2, 3, 4};
array = new SortedArray(20, data);
assertEquals(array.size(), 4);
assertEquals(array.capacity(), 20);
array = new SortedArray(2, data);
assertEquals(array.size(), 4);
assertEquals(array.capacity(), 4);
}
#Test
public void testSize() {
System.out.println("size");
SortedArray arr = new SortedArray(10);
// Array is initially empty
assertEquals(arr.size(), 0);
// Inserting elements increases size
arr.add(12);
arr.add(13);
arr.add(14);
assertEquals(arr.size(), 3);
// Inserting duplicates increases size
arr.add(12);
arr.add(13);
assertEquals(arr.size(),5);
// Fill up the array
for(int i = 0; i < 5; ++i)
arr.add(i);
assertEquals(arr.size(), 10);
// Size does not change when array is full
arr.add(10);
arr.add(11);
assertEquals(arr.size(), 10);
// Removing elements decreases size
arr.remove(0);
arr.remove(1);
arr.remove(2);
assertEquals(arr.size(), 7);
// but removing elements that don't exist doesn't change anything
arr.remove(100);
assertEquals(arr.size(), 7);
// Removing from the empty array doesn't change size.
SortedArray empty = new SortedArray(10);
empty.remove(10);
assertEquals(empty.size(), 0);
}
#Test
public void testCapacity() {
System.out.println("capacity");
SortedArray array = new SortedArray(20);
assertEquals(array.capacity(), 20);
array = new SortedArray(100);
assertEquals(array.capacity(), 100);
Integer[] data = {1,2,3,4,5,6,7,8,9,0};
array = new SortedArray(20, data);
assertEquals(array.capacity(), 20);
array= new SortedArray(5, data);
assertEquals(array.capacity(), 10);
}
#Test
public void testIsEmpty() {
System.out.println("isEmpty");
SortedArray array = new SortedArray(10);
assertTrue(array.isEmpty());
array.add(10);
assertFalse(array.isEmpty());
array.remove(10);
assertTrue(array.isEmpty());
}
#Test
public void testIsFull() {
System.out.println("isFull");
SortedArray array = new SortedArray(5);
assertFalse(array.isFull());
array.add(10);
array.add(11);
array.add(12);
array.add(13);
array.add(14);
assertTrue(array.isFull());
array.remove(10);
assertFalse(array.isFull());
}
#Test
public void testIterator() {
}
testSize Failed : Expected <0> but was <3>
testCapacity Failed : Expected <5> but was <10>
testConstructor Failed : Expected <0> but was <4>
testisFull Failed : jUnit.framework.AssertionFailedError
testisEmpty Failed : jUnit.framework.AssertionFailedError
You forgot to include your "add(T toAdd)" and "remove(T toRemove)" methods, which when I was going through to make the tests pass, was the source of a vast majority of the fails. (Note: a trace of the fails would help, since your adds and removes need to be pretty complicated to fit the design it seems you intend)
Anyways, on to fixing what I can see.
In your second constructor, you never actually assign the data you take in. You call this.array = (T[]) new Comparable[0]; which creates an empty array of type Comparable. In reality, you need to call this.array = data in order to keep what's been given to you.
Another thing, in your size() method you forgot to place a semicolon after this.size. That tends to prevent things from passing.
Finally, final private T[] array can't have final, or you'll never be able to add or remove elements.
As a bonus, here are the add() and remove() methods I used to fit the requirements and make the tests pass (with comments!!!!):
public void add(T t) {
if (!(size >= capacity)) { //If there's room...
if (size == 0) //If the array is empty...
array[0] = t; //Add to first index
else
array[size] = t; //Add to next available index
size++;
}
}
public void remove(T element) {
if (size <= 0) //If the array is empty...
return; //Stop here
else {
for (int i = 0; i <= this.size(); i++) { //Linear search front-to-back
if (array[i].equals(element)) { //Find first match
array[i] = null; //Delete it
size--;
if (i != size) { //If the match was not at the end of the array...
for (int j = i; j <= (this.size() - 1); j++)
array[j] = array[j + 1]; //Move everything after the match to the left
}
return; //Stop here
}
}
}
}
On a side note, your calls to create SortedArray objects should really be parameterized (Using the <> such as SortedArray<Integer> arr = new SortedArray<Integer>(5, data);).
I have an ArrayList of an object from where I want items of a particular position, but everytime I launch the Activity the retrieved position should be randomize and also won't repeat until every position Item is completely retrieved. I used this method:
public static int getRandomNumber(ArrayList<Integer> arr)
throws IllegalArgumentException {
try {
Random random = new Random();
int select = random.nextInt(arr.size());
int randomnum = arr.get(select);
GlobalData.randList.remove(select);
return randomnum;
} catch (IllegalArgumentException e) {
for (int i = 0; i < arr.size(); i++) {
GlobalData.randList.add(i);
}
return 0;
}
but its not working,like duplicate number is coming, there may be a reason because everytime I am re launching the activity. I did it in oncreate instead of onResume but its not working as I expected? Is there any other way to work with it? Any solution?
Use Collections.shuffle() to shuffle the array. Use another variable to track the current position in the array. Each time you retrieve a new value increment the variable. Once you reach the end of the array re-shuffle it.
Reference:
Shuffling algorithms
public class RandomArray {
ArrayList<Integer> array = null;
int position = 0;
public RandomArray(ArrayList<Integer> arr) {
array = arr;
position = arr.size();
}
public int getNext() {
if (position == array.size()) {
position = 0;
Collections.shuffle(array);
}
return array.get(position++);
}
}
If you don't care about the original order, you can try this:
Object[] array = new Object[10]; // say 10 objects
int remain = array.length;
Random rnd = new Random();
public Object next () {
if (remain == 0) {
return null;
} else {
int i = rnd.nextInt(remain--);
Object tmp = array[i];
array[i] = array[remain];
array[remain] = tmp;
return tmp;
}
}
You can also do similar thing with ArrayList.
Well, in this way, it is faster than shuffle() method. shuffle() has the time complexity of O(n) while my code is O(1).