wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array list/linking list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array list/linking list" Watch "Array list/linking list" New topic
Author

Array list/linking list

joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello,

i am working on an ongoing program and at this stage i need to implement something like an array list. This is what i want to do.

1) Create a maximum number of squares.
eg Final int maxSq = 12;
2) Create an empty array list.
3) based on a choice, add 12 textAreas to array list.
problem 1: i seem unable to create a list with existing textAreas.
4) choose a random textArea from the list.
5) change color to blue
6) remove item from list.
(this process requires a simple loop where

for (int currentSq = 0 ; currentSq < maxSq ; currentSq++){
**code to add item to list;randomly select;remove item;}

Currently a lot of is theory and im trying to figure out how to do this. My research has suggested array lists, although i have seen linked list mentioned. However, examples i find of lists are like this:

int[] array = { 1, 2, 3, 4, 5 };

or String array = { "1", "2", "3" }:

i realise that "int" is a number, and "String" is text. But i am unsure how to use existing objects eg jtextArea1, jtextArea2, etc. as it states that jTextArea cant be used (incompatable types). Also, when attempting to add items to a list, my "code" is unrecognised.

eg int[] test = {};
test.add {1};
test.add {2};

im sure this is a syntax problem. So does anyone know of an idiots example anywhere? the books i have dont give me what i need.

thankyou,

joseph
[ February 25, 2005: Message edited by: joseph mcgratton ]
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Hello Joseph,

1 - Look here to learn arrays basics. Briefly, arrays are fixed size container.

2- Take a look at the Java Collections Framework. It may take you a while to understand how it works but that's a mandatory knowledge. Briefly, a collection is a container whom the size can increase over time.

Hope that helps,


Lionel Badiou
CodeFutures Software
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello,

thankyou for your post, it should help me. I understand the basics of how arrays work in general and hopefully this will help me solve my problem. what ill do is go through the pages for a couple of hours after work. then ill create a "dummy" program. any problems and ill post my code here!

thanks,

joseph
Lionel Badiou
Ranch Hand

Joined: Jan 06, 2005
Posts: 140
Hi again,

I have no doubt you know how arrays work yet arrays basics should help you also to learn the correct syntax

Best regards,
joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello.

i have done as suggested and looked at the information on the sun website. There are no examples that are very similar to what i am doing, but i have done what i can with what i learnt. unfortunately it is wrong.



line 1 sets the maximum count for the loop.
line 2 creates the array and creates the objects (references?) in it.
line 3 is wrong. it is not a statement. it is underlined in red. the relavent imports have been added to code.
line 4 is wrong. it is not a statement, it is underlined in red.

the purpose of line 3 is to randomize the references in the list (i believe it changes the value of the 0 position)
the purpose of line 4 is to change the color of the "random" object to blue.

anyone got feedback/suggestions/sample code?

thankyou,

joseph.

p.s. i didnt find anything on removing the item that was selected by the program.
[ February 26, 2005: Message edited by: joseph mcgratton ]
Horatio Westock
Ranch Hand

Joined: Feb 23, 2005
Posts: 221
Hi,

> Object[] test = {sq1, sq2, sq3, sq4, sq5, sq6, sq7, sq8, sq9, sq10};

Why have you choosen Object[] here? What kind of objects are you putting in your array?


>Collections.shuffle(test);

This won't work as test isn't a java.util.List. I think you are getting confused between a simple array and the collection ArrayList.

>test[].setBackground(Color.blue);

Did you mean test[sqCur].setBackground...? If you did, becuase you choose Object as your array type, you would have to cast to whatever type test is really holding. However, I think you are a little confused about this whole thing.

First, you need to read up on some java basics, and read the array tutorial again. Once you have that, try writing some pseudo code for what you are trying to acheive. I don't understand completely what you are doing, but here goes,

1. Create a number of squares (java.awt.TextArea?) and put them in an array
2. ??based on a choice, add 12 textAreas to array list??
3. Choose a random textArea from the list.
change it's color to blue
remove item from list

I'm not sure whether you just want to do step three once or for every square.

So, here is an example using ArrayList from the java Collections framework (see links provided above).

I thoroughly recommend you do go back and fully understand basics, arrays and the java Collections tutorial. Then go on to look at the sample below and look up the API docs for things like ArrayList and Random.


imports:

import java.awt.Color;
import java.awt.TextArea;
import java.util.ArrayList;
import java.util.Random;

code sample:

joseph mcgratton
Ranch Hand

Joined: Feb 15, 2005
Posts: 41
hello,

thankyou for your time and reply. you have helped to explain more about arrays to me. Some of the elements you have stated are similar to something i found in a recent webpage about a random number generator using lists and shuffling. I dont know why, but if if someone is to give me a book with examples and answers i can find it hard to understand and to relate to a current project. But if someone explains in terms of my current project, i can understand it 100% and apply it to any future projects. an example is creating a grid. i could NEVER understand it after many books and articles, yet when i was explained in relation to my project, i understood and can now apply it to future projects (eg a grid within a grid).
you said you werent sure about what i was trying to achieve. unfortunately its hard to explain and not plausable to show all code - its over 2000 lines long because although i understand teh theory behind such things as methods, i am unable to apply it because i cant relate it to my personal project - out of 2000 lines, 1200 - 1400 is probably cut and paste.
anyway thanks for your help, and ill let you know how i get on!

Joseph
 
wood burning stoves
 
subject: Array list/linking list