By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,778 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,778 IT Pros & Developers. It's quick & easy.

Java arrays

P: 5
Hi im new to java and i am currently trying to use arrays. Can someone help me find a way to input a number into the array and then push the number to the next slot if you input another number. The push and pull method from the left to the right.
Nov 30 '06 #1
Share this Question
Share on Google+
11 Replies


DeMan
100+
P: 1,806
do you have to use an array? Java has some nice Structures in java.util which will function exactly how you want (or you could build your own LinkedList). In particular Java has a Vector() which has all the normal Container methods (get(), add(), addAll(), remove(), for most of which you can specify the index of the item (which you can find using indexOf() (- careful, check the element exists in the structure first). If memory serves me correctly there is also a double ended stack (which if there isn't is trivial to implement using a vector()) which could give you exactly what you want. Using an array would be inefficient and tedious, because arrays tend not to have dynamic size.
Nov 30 '06 #2

P: 5
Well i am just a beginner so i dont actually know how to do the other methods. Right now im just trying to familiarize myself with arrays and it would be great if someone has a solution.
Dec 1 '06 #3

10K+
P: 13,264
Well i am just a beginner so i dont actually know how to do the other methods. Right now im just trying to familiarize myself with arrays and it would be great if someone has a solution.
Post what you have so far.
Dec 1 '06 #4

P: 5
public class Array2k{
public static void main(String[]args){


int number;
int i;
int pushnum = 0;
int pullnum = 0;


int[] integers = new int[10];
for (i = 0; i < 10; i++){

TextIO.put("What would you like to do? (1 for push, 2 for pull)");
number = TextIO.getlnInt();

if (number == 1) {
TextIO.putln ("Enter the number you wish to push ") ;
pushnum = TextIO.getlnInt();
}
else if (number == 2) {
TextIO.putln ("Enter the number you wish to pull ") ;
pullnum = TextIO.getlnInt();

}
integers [9 - i] = pushnum;

System.out.print( integers[0]);
System.out.print( integers[1]);
System.out.print( integers[2]);
System.out.print( integers[3]);
System.out.print( integers[4]);
System.out.print( integers[5]);
System.out.print( integers[6]);
System.out.print( integers[7]);
System.out.print( integers[8]);
System.out.print( integers[9]);
}
}
}

So far i can only push numbers from right to left. Im trying to do it for left to right.
Dec 1 '06 #5

10K+
P: 13,264
public class Array2k{
public static void main(String[]args){


int number;
int i;
int pushnum = 0;
int pullnum = 0;


int[] integers = new int[10];
for (i = 0; i < 10; i++){

TextIO.put("What would you like to do? (1 for push, 2 for pull)");
number = TextIO.getlnInt();

if (number == 1) {
TextIO.putln ("Enter the number you wish to push ") ;
pushnum = TextIO.getlnInt();
}
else if (number == 2) {
TextIO.putln ("Enter the number you wish to pull ") ;
pullnum = TextIO.getlnInt();

}
integers [9 - i] = pushnum;

System.out.print( integers[0]);
System.out.print( integers[1]);
System.out.print( integers[2]);
System.out.print( integers[3]);
System.out.print( integers[4]);
System.out.print( integers[5]);
System.out.print( integers[6]);
System.out.print( integers[7]);
System.out.print( integers[8]);
System.out.print( integers[9]);
}
}
}

So far i can only push numbers from right to left. Im trying to do it for left to right.
I can't see what you are trying to do here. Explain what you mean by pushing a number from left to right. Do you mean shifting elements of an arra to the rigth by one?
Dec 1 '06 #6

P: 32
I think you'd be much better using linked lists, shifting the values in an array to the right when adding a new value invites problems with the array's size limit, and causes you to have to reassign multiple values at once, (the code will take longer and longer to run with large arrays holding many pieces of data.) Its an issue of efficiency. It is technically possible to do it, but you'd have to keep track of the numer of entries in the array and you'd have to create a loop to move all the data values to the right.

If you still insist on using an array for this, make sure you increment an integer variable by one for every entry you add to the array (I'll call it numValues). if the array is empty simply add the value to the first slot, if its not empty run a loop with the folllowing properties:

Important: Check if your array is full before running this loop or your program will crash.
count variable starts at NumValues-1 (can also start AT Numvalues, but make sure the array position references are correct)
check if count is -1 before continuing loop
for each iteration:
make array[count+1] equal to array[count]
decrement count

after the loop runs you are free to write to array[0].
Dec 2 '06 #7

P: 32
Linked lists are objects that contain a field that holds a reference to another object of the same type. If you didn't understand that, read this code, hopefully it will help.

The following Code is a very simple linked list where each node contains a String as its data.
Expand|Select|Wrap|Line Numbers
  1. public class StringNode
  2. {
  3.     /*the following variables are fields, 
  4.     *they will hold the values you want to 
  5.     *contain in this object
  6.     *
  7.     *more importantly pay attention to the 
  8.     *"next" field, it references another
  9.     *StringNode object. This is what gives 
  10.     *this object its list behavior
  11.     */
  12.     public String data;
  13.     public StringNode next;
  14.  
  15.     //constructors
  16.     public StringNode(String s){
  17.     data=s;
  18.     next=null;
  19.     }
  20.     public StringNode(String s, StringNode other){
  21.     data=s;
  22.     next=other;
  23.     }
  24. }
  25.  
You could create a list object or simply use the Node object type as the list.
You can insert values in any point in this list, just remember that anything that loses its reference becomes garbage and its gone.

you create most objects in java like this
Object name= new Object(constructor_inputs)

StringNode head= new StringNode("foo", new StringNode("foo2"))
creates a variable, head that references an object with data="foo", that object's next field references an object with data="foo2" whose next="null"
This can be shown with the abstract representation:
head -> "foo" -> "foo2" -> [null]

unfortunately you can't directly acess a particular spot like you can with array[int], but you CAN "walk through" the list and sample the data until you find what you want.


the basic way to walk through a list and find a piece of data:
if you take a temporary StringNode named, say current, give it a reference to your list with the argument current=head; and put it into your loop, check current or current.next to see if its null (this can make a difference depending on what you plan to do to the list)
inside the loop use an if statement on current.data
at the end of each iteration set currrent = current.next

This is a very basic use of linked lists, there are many, more complex uses for them.
if you have the following list
head -> "foo" -> "foo2" -> [null]

and you want to add an element to the end, use a loop and walk through the list until you find the list element whose next field is null,
then current.next = new StringNode("element")

if you want to add insert an element between two values, you walk through the list to the first value
StringNode temp =current.next
// Doing that keeps track of the rest of the list so it doesnt disappear
current.next = new StringNode("element");
// this puts a new element onto the back of the current node
current.next.next=temp;
//this tacks the rest of the list on the back of the new element

I'll leave you to figure out some more for yourself.


P.S. I should mention I haven't learned to deal with unwanted conditions inside the constructor yet. If you don't want a null passed to this constructor, you have to figure out how to fix it.
Dec 2 '06 #8

P: 32
I forgot to mention the case when you add to the front of the list,

to add to the front, create a temp like before,
then either set head equal to new StringNode("element",temp) or StringNode("element")
If you don't use the constructor to set the next field, then set the head.next field equal to temp
Dec 2 '06 #9

DeMan
100+
P: 1,806
This sounds suspiciously like a double ended queue......
Dec 5 '06 #10

P: 32
I dont even know what a double ended queue is. I'm only explaining what I learned in class. I might not have done it in the most efficient way. I apologize if something was modified so that it does not follow linked list structure anymore.
Dec 7 '06 #11

P: 32
I went and looked up double-ended queue, and ordinary queue and lo and behold, the implementation I found used linked lists. The doubled-ended is equivalent to a linked list where you define the variable tail as the last item in the list and add the field prev to hold the previous node
Dec 7 '06 #12

Post your reply

Sign in to post your reply or Sign up for a free account.