Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8233587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:10:50+00:00 2026-06-07T18:10:50+00:00

Basically, I need to add items to the arraylist. Am I correct in using

  • 0

Basically, I need to add items to the arraylist. Am I correct in using a separate class (this is in part what I am practicing here) or is that a stupid idea because it is basically just an arraylist. I thought I would create a class because I want to randomise additions to it, etc. Alternately, since it is just an arraylist class, should I use “extends arraylist” or something similar??? Code examples most welcome. I am obviously missing some crucial programming theory here. Thank you.

NOTE I have removed what I think is unrelated code
I have a main:

public class cgame2 extends Activity {

private ArrayList<Integer> cToChooseFromImages; // clear image list

colourlist colourlistused = new colourlist();
colourlist userscolourlist = new colourlist();

....truncated....towards the end of my code I have......
colourlistused.randomaddnewitem();

And I have a class (called colourlist):

import java.util.ArrayList;
import java.util.Random;
import android.util.Log;

public class colourlist {

private ArrayList<Integer> clist;

private int picked;

int imagetodisp;

public colourlist() {
}

public void randomaddnewitem() {
    Random randomtouse = new Random(System.nanoTime());
    picked = randomtouse.nextInt(3);
    clist.add(picked); // this is where the program crashes according to log info
}
public void addnewitem(int itemtoadd) {
    clist.add(itemtoadd);
}

.....etc.....
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T18:10:51+00:00Added an answer on June 7, 2026 at 6:10 pm

    If you’re adding functionality to ArrayList, the best way is just the way you have done it.

    Create a member ArrayList of your class and create functions to extend the behavior. This is called Composition.
    Contrast with Inheritance, or creating a subclass of ArrayList, which can create more problems by unnecessarily coupling your implementation to ArrayList.

    Don’t forget to initialize the list. The best way would be:

    List<Integer> clist = new ArrayList<Integer>();
    

    Use the interface List to make your code more flexible if you choose to change the specific list implementation later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add some items together using jquery, how would i go about
basically need to change the value that - admin_url() returns any idea?
I basically need to check if there is an easier way to do this
I have a page that displays a list of items. My markup is basically
Is there a faster way? I basically need to add AA-ZZ to thousands of
I'm modifying an accordion script to add the class active to the link that
I'm trying figure out if this is possible. Basically, I need to create an
Basically need to generate custom(some different then yes no) messeges(alert) in JS , how
I basically need to get user input: gets.chomp(input?) And then to convert the given
I basically need a custom function to be used only when, for example, the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.