ArrayIndexOutOfBounds Exception thrown - java

public Pasient[] finnPasient(String dato)
{
int j = 0;
Pasient[] p = new Pasient[j];
for(int i = 0; i < pasienter.length; i++)
{
if(pasienter[i] != null && pasienter[i].getFødselsdato().equals(dato))
{
p[j] = pasienter[i];
j++;
}
}
if(j == 0)
return null;
else
return p;
}
that is my method. I think I know what's wrong, but I am not sure how to fix it. I think that the arraylength does not update while the loop is running. Pasienter.length is always 100 for my tests. Ask me if you need any more info to respond, thanks

int j = 0;
Pasient[] p = new Pasient[j];
j is 0, p has no elements at all, so the first element which is at index 0, is out of bounds, this probably happens at the line:
p[j] = pasienter[i];
↑
When you write j++, this doesn't enlarge the array that you've already created. Remember, array's size cannot be changed.
Since you don't know how long should the array be, I advise you to read about ArrayList and do something like:
ArrayList<Pasient> p = new ArrayList<>();
And then,
p.add(pasienter[i]);

Related

Sort JSONArray with single for loop

I have a JSONArray with 5 different JSONObject and each of the 5 JSONObject has an identifier string value. The five values are "aa","bb","erer","cc","gg". My requirement is to get the JSONObject with identifier "erer" to first place and the following JSONObjects can be in any order.
The json is:
{
"obj":[
{"identifier":"aa",},
{"identifier":"bb",},
{"identifier":"erer",},
{"identifier":"cc",},
{"identifier":"gg",}
]
}
The final result has to be "erer","aa","bb","cc","gg" and I need to do this in a single loop.
I'm able to do this in two loops.
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject j = jsonArray.getJSONObject(i);
if(j.getString("identifier").equals("erer")) {
sortedJson.put(joPayLoad);
}
}
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject j = jsonArray.getJSONObject(i);
if(!j.getString("identifier").equals("erer")) {
sortedJson.put(joPayLoad);
}
}
But the JsonArrray might also have 10,000 JSONObjects. And so, this 'two for-loops' will cause a performance lag. So, please help me to achieve my above said requirement in a single loop.
Thanks in advance!
Given JsonArray is a List (source) you can just try to swap the value with erer to be the new first element:
for (int i = 0; i < jsonArray.length(); i++) {
JsonValue j = jsonArray.get(i);
if(j.getString("identifier").equals("erer")) {
JsonValue tmp = jsonArray.get(0);
jsonArray.set(0, j);
jsonArray.set(i, tmp);
break;
}
}
Haven't tried it since I don't have EE installed, but maybe you can make it work with the methods provided.
You need two pointers, a read pointer and a write pointer. Both start at the last item. Read the element at the read pointer. If it's "erer," then just decrement the read pointer, otherwise, write the element to the write pointer and decrease both the read pointer and the write pointer. At some point the read pointer will decrease to zero. If the write pointer is >0 it means you found some "erer" elements that you did not write. Write them to the write pointer and decrement the write pointer until it too is zero.
String[] data = { "aa", "gg", "dd", "ee", "erer", "gg", "erer" };
int r = data.length - 1, w = data.length - 1;
while (r >= 0) {
if (!"erer".equals(data[r])) {
data[w] = data[r];
w--;
}
r--;
}
while (w >= 0) {
data[w] = "erer";
w--;
}
Okay, technically there's still two loops here, but it should be clear that we're iterating only once over the array. If you really gotta have one loop, this would work...
int r = data.length - 1, w = data.length - 1;
while (w >= 0) {
if (r >= 0) {
if (!"erer".equals(data[r])) {
data[w--] = data[r];
}
r--;
} else {
data[w--] = "erer";
}
}

why does my java code print out [ ] when I insert 2 numbers in it

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class forA1 {
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
String []num = sc.nextLine().split(" ");
int n = Integer.parseInt(num[0]);
int m = Integer.parseInt(num[1]);
while(n != 0){
// initialize the queue
Queue<Integer> queue = new LinkedList<Integer>();
queue.clear();
for (int i = 1; i <= n; i++)
queue.add(i);
//int res = 0;
while (!queue.isEmpty()) {
for (int i = 0; i < m-1; i++)
queue.add(queue.remove());
queue.remove();
}
//System.out.println(res);
//System.out.println(res);
System.out.print(queue);
String []num2 = sc.nextLine().split(" ");
n = Integer.parseInt(num2[0]);
m = Integer.parseInt(num2[1]);
}
}
}
When I insert 2 numbers, it will print out [].Can some please explain to me why this happens, I'm a student, so I apologize if I'm asking a question that is easy for others.
Not sure what you're trying to do with the queue, but since you have the condition while (!queue.isEmpty()), no matter what you do inside the loop (unless you have some kind of break logic, thanks to #Andy's comment) you'll either never leave it (i.e. an infinite loop), or get an empty queue after it.
If you change from
while (!queue.isEmpty()) {
for (int i = 0; i < m-1; i++)
queue.add(queue.remove());
queue.remove();
}
to
for (int i = 0; i < m-1; i++)
queue.add(queue.remove());
queue.remove();
You will be able to see some non-empty output (given appropriate m and n values).
Firstly, Since you aren't decrementing the value of 'n', it's an infinite loop so far proper input is provided viz. the values of 'n' & 'm':-
while(n != 0){
...
// lines of code
String []num2 = sc.nextLine().split(" ");
n = Integer.parseInt(num2[0]);
m = Integer.parseInt(num2[1]);
}
Secondly, for your question- "When I insert 2 numbers, it will print out []. Can some please explain to me why this happens?" Actually, you have written a block of code in your program as below:-
while (!queue.isEmpty()) {
for (int i = 0; i < m-1; i++)
queue.add(queue.remove());
queue.remove();
}
Last 2 lines of this block of code (just before closing braces) have counter effect on the queue. It would make your queue empty (You can check the same by putting - "System.out.println(queue);" before & after this while loop, you will understand more clearly by doing the same). As a result, you are getting "[]" as your output (in the console window) which is empty Queue !!
PS:- In Java, we always use "System.out.println();" to debug wherever we wish to understand the flow of the program.

How to remove object from array

Please bear within as it might be difficult to understand
I have an array of jbuttons 50 size big, for this example ill use 7 I have jbutton object within 1 2 3 4 6 7 but not 5. These are printed on the screen. I want to remove these jbuttons however all buttons up to 5 are removed while the last two are not.
for(int i = 1; i < 51; i++){
if(seat.buttonArray[i] == null){
remove(seat.buttonArray[i]);
seat.buttonArray[i] = null;}
}
There is no way to remove element from array, assuming you want latter indexes changed after remove. For this purpose, you should use List:
Iterator buttonIterator = seat.buttonList.iterator();
while (buttonIterator.hasNext()) {
Object button = buttonIterator.next(); //or more specific type, if your list was generified
if (button == null) { //or some other criteria, wrote this just as an example
buttonIterator.remove();
}
}
If using array is mandatory, you have two options:
Set seat.buttonArray[i] to null value, indicating it has been removed;
Recreate array each time you deleted something. See System.arraycopy javadoc for details, although I do not recommend this approach because of performance considerations.
You could store the values in a temp array and then copy what you want back into your original array. Somewhat similar to this:
int myArray[50];
int temp[50];
int good;
for (int i = 0; i < 50; i++) {
myArray[i] = i;
}
for (int i = 0; i < 50; i++) {
temp[i] = myArray[i];
}
good = 0;
for (int i = 0; i < 50; i++) {
if (i < 10) {
} else {
myArray[good] = temp[i];
good += 1;
}
}
Looks messier than I first thought... But it essentially does what you're wanting.

Rearranging array when it has a null position

I have this code that searches one object in an array and removes it. I'm having a problem with its position, since some other methods work with this array (and it gives me a NullPointerException every time). My method looks like this:
public void deleteHotel(String hotelName) {
for (int i = 0; i < this.hoteis.length; i++) {
if (this.hoteis[i].getName().equalsIgnoreCase(nomeHotel)) { //searches the array, looking for the object that has the inputted name
this.hoteis[i] = null; //makes that object null
if (this.hoteis.length > 1 && this.hoteis[this.hoteis.length - 1] != null) { //for arrays with lenghts bigger than 1 (since there's no problem with an array with one position)
for (int x = i; x < this.hoteis.length; x++) {
this.hoteis[x] = this.hoteis[x + 1]; //makes that null position point to the next position that has an object, and then that position points to the object in the next position and so on
}
this.hoteis[this.hoteis.length - 1] = null; //since the last to positions will be the same, make that last one null
Hotel[] hoteisTemp = new Hotel[this.hoteis.length - 1];
for(int x = 0; x < this.hoteis.length - 1; x++){ //create a new array with one less position, and then copy the objects on the old array into the new array, then point the old array to the new array
hoteisTemp[x] = this.hoteis[x];
}
this.hoteis = hoteisTemp;
}
i = this.hoteis.length;
}
}
}
When I use other methods (for example, one that returns the implemented toString()s of each object) it gives me a NullPointerException. Can you guys identify the error in the code? Much appreciated...
I have tested your function and I see what you mean by it getting a nullpointerexception, this is due to the array not resizing the list - which is due to your conditional:
if (this.hoteis.length > 1 && this.hoteis[this.hoteis.length - 1] != null).
Simply removing this solved the issue, here is the working function:
public static void deleteHotel(String hotelName) {
for (int i = 0; i < hotels.length; i++) {
if (hotels[i].getName().equalsIgnoreCase(hotelName)) { //searches the array, looking for the object that has the inputted name
hotels[i] = null; //makes that object null
for (int x = i; x < hotels.length -1; x++)
hotels[x] = hotels[x + 1]; //makes that null position point to the next position that has an object, and then that position points to the object in the next position and so on
Hotel[] hoteisTemp = new Hotel[hotels.length - 1];
for(int x = 0; x < hotels.length - 1; x++) //create a new array with one less position, and then copy the objects on the old array into the new array, then point the old array to the new array
hoteisTemp[x] = hotels[x];
hotels = hoteisTemp;
break;
}
}
}
Though please consider using a list of some sort when needing to use a list with a changing size.
The fundamental problem is that you're not allowing for where you removed the entry from the array.
Instead of
for(int x = 0; x < this.hoteis.length - 1; x++){
you want
for(int x = 0; x < this.hoteisTemp.length; x++){
(although that's a style choice)
and more significantly, instead of
hoteisTemp[x] = this.hoteis[x];
you want
int y = x < i ? x : x + 1;
hoteisTemp[x] = this.hoteis[y];
You also want to get rid of everywhere you're setting array elements to null, because if your copying logic works correctly, that's unnecessary.
For this use case, I would consider using one of the List implementations.
Consider rewriting your code
List result = new ArrayList();
for (int i = 0; i < this.hoteis.length; i++) {
if (!this.hoteis[i].getName().equalsIgnoreCase(nomeHotel)) {
result.add(this.hoteis[i]);
}
}
return result.toArray();
The point where you're shifting the array elements towards the left
for (int x = i; x < this.hoteis.length; x++) {
this.hoteis[x] = this.hoteis[x + 1];
}
The loop condition should be x < this.hoteis.length - 1 because at the last iteration when x = this.hoteis.length - 1 the index value this.hoteis[x + 1] would throw a NullPointerException.
Try using ArrayList it will simplify your code complexity.Here is the link to documentation.
http://docs.oracle.com/javase/6/docs/api/java/util/ArrayList.html

Why am I getting an array index out of bounds exception?

Basically, I have an assignment that requires me to find the mode of a given set of numbers.
This is my Method:
public void findMode (){
/* The vector data is analyzed and transferred into a smaller vector
smallList (0..100). For each occurrence of n in vector data,
smallList[n] is incremented +1. function Largest is then called
to find the largest quantity in vector smallList. The mode(s)
is/are printed out. */
int loop, largest;
int[] smallList = new int[101];
for (int i = 0; i < myHowMany; i++)
{
smallList[myData[i]]++;
}
int max = 0;
for (int i = 0; i < smallList.length; i++)
{
if (max < smallList[i])
{
max = smallList[i];
}
}
//Max is 26
int size = 0;
for (int i = 0; i < smallList.length; i++)
{
if (i == max) size++;
}
int[] modes = new int[size];
int modeIndex = 0;
for (int i = 0; i < smallList.length; i++)
{
if (smallList[i] == max)
{
modes[modeIndex] = smallList[i];
System.out.println(modes[modeIndex]);
modeIndex++;
}
} Everything compiles fine, but when I run this method, I get an out of bounds array method. I have no idea WHY this happens so I
need to know if the community can help me
.
Solved!
Please tell me if I need more information!
edit: I forgot to mention that I get the error here:
modes[modeIndex] = smallList[i];
New Problem:
I fixed the problem from before, but now, I find that my max goes unto the array so that modes = 26(max)
Your error is in this line
if (i == max) size++;
It should be
if (smallList[i] == max) size++;
This is causing the size of modes to be wrong
That should be clear enough: either modeIndex or i exceeds the array size. Since you're looping over smallList with smallList.length, I guess the error is in the modeIndex then. In this case, size (which is used to construct modes) isn't big enough.
if (i == max) size++;
and then
if (smallList[i] == max)
Please check your value for size.

Categories