Chat-Applikation/src/server/ArrayHelper.java

67 lines
2 KiB
Java

/*
* Copyright (C) 2021 eichehome
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server;
/**
*
* @author eichehome
*/
public class ArrayHelper {
public String[] Threads;
public void pushThread(String message) {
if (Threads != null) {
String[] temp = new String[Threads.length + 1];
for (int i = 0; i < Threads.length; i++) {
temp[i] = Threads[i];
}
temp[temp.length - 1] = message;//Letzter eintrag
Threads = temp;
} else {
String[] temp = new String[1];
temp[temp.length - 1] = message;
Threads = temp;
}
}
public String popThread(String thread) {
if (Threads != null) {
int index = getIndex(thread, Threads);
String result = Threads[index];
String[] temp = new String[Threads.length - 2];
for (int i = 0; i < index; i++) {
temp[i] = Threads[i];
}
for (int i = ++index; i < temp.length; i++) {
temp[i - 1] = Threads[i];
}
return result;
} else {
return "";
}
}
public int getIndex(String match, String[] array) {
for (int i = 0; i < array.length; i++) {
if (array[i] == match) {
return i;
}
}
}
}