• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array list/linking list

 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
joseph mcgratton
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic