Halloo
habe zur Zeit ein Problem und zwar habe ich ein Array mit 100 freien Plätzen. Die Aufgabe ist nun dort werte einfügen zu können und auch ersetzen zu können. Derunterscheid besteht darin, dass beim einfügen der evtl vorhandene Wert im Array nicht gelöscht sondern verschoben wird.
Wie kann man die Werte richtig rotieren lassen? Bei meiner Methode zeigt er immer einen Fehler beim ausführen an.
Beim löschen wird mein Weert nur mit einer 0 überschrieben. habe keine andere Möglichkeit gefunden meine andere idee den stack auf "null" zu setzen hat er als fehler gemarkert :/
Vielen Dank schonmal für eure Hilfe
habe zur Zeit ein Problem und zwar habe ich ein Array mit 100 freien Plätzen. Die Aufgabe ist nun dort werte einfügen zu können und auch ersetzen zu können. Derunterscheid besteht darin, dass beim einfügen der evtl vorhandene Wert im Array nicht gelöscht sondern verschoben wird.
Code:
public class ArrayFrame extends javax.swing.JFrame {
int[] werte = new int[100];
int numWerte = 0;
void listeDarstellen()
{
String neu = "";
for (int i = 0; i < this.numWerte; i++)
{
neu += "" + this.werte[i] + "\n";
}
taAusgabe.setText(neu);
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
jScrollPane1 = new javax.swing.JScrollPane();
taAusgabe = new javax.swing.JTextArea();
jLabel2 = new javax.swing.JLabel();
tfPosition = new javax.swing.JTextField();
jLabel3 = new javax.swing.JLabel();
tfWert = new javax.swing.JTextField();
bErsetzen = new javax.swing.JButton();
bEinfuegen = new javax.swing.JButton();
bLoeschen = new javax.swing.JButton();
bRotieren = new javax.swing.JButton();
bOrdnen = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Array-Test");
jLabel1.setText("Die Werte");
taAusgabe.setColumns(20);
taAusgabe.setRows(5);
jScrollPane1.setViewportView(taAusgabe);
jLabel2.setText("Position");
tfPosition.setText("0");
jLabel3.setText("Wert");
tfWert.setText("123");
bErsetzen.setText("Ersetzen");
bErsetzen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bErsetzenActionPerformed(evt);
}
});
bEinfuegen.setText("Einfuegen");
bEinfuegen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bEinfuegenActionPerformed(evt);
}
});
bLoeschen.setText("Loeschen");
bLoeschen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bLoeschenActionPerformed(evt);
}
});
bRotieren.setText("Rotieren");
bRotieren.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bRotierenActionPerformed(evt);
}
});
bOrdnen.setText("Ordnen");
bOrdnen.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
bOrdnenActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tfWert, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
.addComponent(tfPosition, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
.addComponent(bErsetzen, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
.addComponent(bEinfuegen, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
.addComponent(bLoeschen, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
.addComponent(bRotieren, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)
.addComponent(bOrdnen, javax.swing.GroupLayout.DEFAULT_SIZE, 118, Short.MAX_VALUE)))
.addComponent(jLabel1))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(tfPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(tfWert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bErsetzen)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bEinfuegen)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bLoeschen)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bRotieren)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(bOrdnen))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE))
.addContainerGap())
);
pack();
}// </editor-fold>
private void bErsetzenActionPerformed(java.awt.event.ActionEvent evt) {
String s = tfPosition.getText();
int pos = Integer.parseInt(s);
s = tfWert.getText();
int wert = Integer.parseInt(s);
listeDarstellen();
}
private void bEinfuegenActionPerformed(java.awt.event.ActionEvent evt) {
String s = tfPosition.getText();
int pos = Integer.parseInt(s);
s = tfWert.getText();
int wert = Integer.parseInt(s);
werte[pos] = wert ;
listeDarstellen();
}
private void bRotierenActionPerformed(java.awt.event.ActionEvent evt) {
for (int i=0;i<werte.length;i++)
{
werte[i]= werte[werte.length];
werte[i+1] = werte[i-1];
}
listeDarstellen();
// TODO add your handling code here:
}
private void bLoeschenActionPerformed(java.awt.event.ActionEvent evt) {
String s = tfPosition.getText();
int pos = Integer.parseInt(s);
if (numWerte>pos+1)numWerte--;
werte[pos] = 0 ;
listeDarstellen();
}
private void bOrdnenActionPerformed(java.awt.event.ActionEvent evt) {
Arrays.sort(werte);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ArrayFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton bEinfuegen;
private javax.swing.JButton bErsetzen;
private javax.swing.JButton bLoeschen;
private javax.swing.JButton bOrdnen;
private javax.swing.JButton bRotieren;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea taAusgabe;
private javax.swing.JTextField tfPosition;
private javax.swing.JTextField tfWert;
// End of variables declaration
}
Wie kann man die Werte richtig rotieren lassen? Bei meiner Methode zeigt er immer einen Fehler beim ausführen an.
Beim löschen wird mein Weert nur mit einer 0 überschrieben. habe keine andere Möglichkeit gefunden meine andere idee den stack auf "null" zu setzen hat er als fehler gemarkert :/
Vielen Dank schonmal für eure Hilfe